I am working on a products archive page where I show 20 products on each page with pagination.
Each product's price is calculated by a formule which gets his parameters by data saved in the database. It works this way (and not saved in custom fields with each product) because then we can change some data in our database and all the products prices will be changed.
The problem is that I now cannot order by meta key and value. So I have tried to add the price to the queried posts array and then order it by the price. But if you use pagination, you only order the first 20 items by price. So the next page shows products with a lower price than products on the first page.
First page 20 item:
- Product 10,00
- Product 12,00
- Product 15,00
- Product 18,00
- etc.
Second page 20 items:
- Product 11,00
- Product 14,00
- Product 15,00
- Product 19,00
- etc.
So I think I need to modify the query in an earlier stage, for example with the 'orderby' parameter. But I can only find orderby custom fields, and this is not what I am searching for.
Does anyone know it there is a hook to add a custom orderby function to the wp query?
My code:
// Get pages page for pagination
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
// Query arguments
$args = array(
'post_type' => 'products',
'posts_per_page' => 20,
'paged' => $paged
);
// Create object
$products = new WP_Query( $args );
// Add price to each product
foreach( $products->posts as $product ) {
$price = price_calulate_function( $product->ID );
$product->price = $price;
}
// Sort by price
uasort( $products->posts, function( $a, $b ) {
$a = $a->price;
$b = $b->price;
if( $a == $b ) {
return 0;
}
return ( $a < $b ) ? -1 : 1;
});
// Loop
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
// Show product
}
}
I am working on a products archive page where I show 20 products on each page with pagination.
Each product's price is calculated by a formule which gets his parameters by data saved in the database. It works this way (and not saved in custom fields with each product) because then we can change some data in our database and all the products prices will be changed.
The problem is that I now cannot order by meta key and value. So I have tried to add the price to the queried posts array and then order it by the price. But if you use pagination, you only order the first 20 items by price. So the next page shows products with a lower price than products on the first page.
First page 20 item:
- Product 10,00
- Product 12,00
- Product 15,00
- Product 18,00
- etc.
Second page 20 items:
- Product 11,00
- Product 14,00
- Product 15,00
- Product 19,00
- etc.
So I think I need to modify the query in an earlier stage, for example with the 'orderby' parameter. But I can only find orderby custom fields, and this is not what I am searching for.
Does anyone know it there is a hook to add a custom orderby function to the wp query?
My code:
// Get pages page for pagination
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
// Query arguments
$args = array(
'post_type' => 'products',
'posts_per_page' => 20,
'paged' => $paged
);
// Create object
$products = new WP_Query( $args );
// Add price to each product
foreach( $products->posts as $product ) {
$price = price_calulate_function( $product->ID );
$product->price = $price;
}
// Sort by price
uasort( $products->posts, function( $a, $b ) {
$a = $a->price;
$b = $b->price;
if( $a == $b ) {
return 0;
}
return ( $a < $b ) ? -1 : 1;
});
// Loop
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
// Show product
}
}
Share
Improve this question
asked Nov 25, 2015 at 10:20
RobbertRobbert
1,2756 gold badges23 silver badges47 bronze badges
5
|
1 Answer
Reset to default 0I fixed my problem with a button to update all the prices in one time.
updated_postmeta
action to trigger the update proccess. – cybmeta Commented Nov 25, 2015 at 12:09