0

So I have this

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 12,
    'category' => 1165,
    'meta_query' => array(
        'meta_key' => 'price_special',
        'value' => array('New Boat', 'Under Offer', 'Reduced', 'Just Added', 'Coming soon', 'New Build'),
        'compare' => 'IN',
        ),
    'suppress_filters' => false,
    'meta_key' => 'price',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'paged' => $paged,
);
$query = new WP_Query($args);

but the meta_query is having no effect on the number of results and therefore the number of pages, can anyone tell me where I am going wrong please?

3
  • this query is going to be extremely slow and expensive, you should query via a taxonomy term not a meta field, and you should query for what you want, not what you don't want. Asking the database to do NOT style queries does not scale. E.g. a longwinded list of every value price_special can have except for Sold would be much faster. Also, get_posts is uncached by default, you should include 'suppress_filters' => false in your args. You also do not need to pass $custom_posts into new WP_Query as you're doing twice as many queries as you need, new WP_Query( $args ) works just fine
    – Tom J Nowell
    Commented Apr 26, 2022 at 18:49
  • Thx Tom, taken that on board and amended. The pagination now does return a lesser number of results as expected. Still getting some odd (displayed results) but you are right get_posts() appears to ignore $args and just get everything, thanks
    – MikeKJ
    Commented Apr 27, 2022 at 9:43
  • WP_Query expects an array of arguments, not an array of posts, what you did was the same as new WP_Query( get_posts( $args ) ) which is super weird and definitely does not work
    – Tom J Nowell
    Commented Apr 27, 2022 at 10:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.