0

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

    }

}
  • Then only way, I think, is sorting by a calculated column but not sure if you can inject such clause in the WP_Query, if you will need a custom query or the possible performance implications it may have. Why storing the final price in a custom field is a problem? I think rebuild the final price for all products if needed is easier and faster. – cybmeta Nov 25 '15 at 11:07
  • Thanks for your answer. I isn't that easy. The price of a product is made up of parts which are posts in another post type with a price value in custom field and some option database values. – Robbert Nov 25 '15 at 11:15
  • When a part's price will be updated, this needs to be updated is all the product's prices. So if I choose this option, I need to make a button to update this prices, or schedule a cron job. What do you think? – Robbert Nov 25 '15 at 11:19
  • If the price parts are stored in custom fields, you could hook into updated_postmeta action to trigger the update proccess. – cybmeta Nov 25 '15 at 12:09
  • Thanks, maybe I will implement this as well. But I have now fixed it with a button in my admin which updates all the prices in one time. – Robbert Nov 25 '15 at 12:11
0

I fixed my problem with a button to update all the prices in one time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.