0

I want to make custom query loop with posts_per_page limit.

<?php
    $news = get_category_by_slug('news');

    $sidebar_related = new WP_Query([
            'category__not_in' => [$news->term_id],
            'post__not_in' => [$post->ID],
            'posts_per_page' => 4,
            'post_status' => 'publish'
    ]);

    if($sidebar_related->have_posts()) :
        while($sidebar_related->have_posts()) : 
            $sidebar_related->the_post();

            the_title();

        endwhile; 

        wp_reset_postdata();
    endif; 

This code returns me 10 (or another value) posts instead of 4.

$sidebar_related->request shows

"SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.ID NOT IN (15190) AND ( wp_posts.ID NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (620) ) ) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 4"

As you can see, there is LIMIT 4. But $sidebar_related->posts shows Array with 11 elements.

  1. I've tried remove all plugins: nothing happens
  2. I've tried to install my theme on empty blog: works correctly
  3. I've tried to remove all caches (object cache, wp_query cache): nothing happens
  4. I've tried to change category__not_in arg to category__in: works correctly
  5. Add wp_reset_postdata(); before the loop: nothing happens

So I can't understand why it works this way. Any help will be appreciated

1

Ok, finally I've found the reason.

We must ignore sticky posts in WP_Query this way:

 $sidebar_related = new WP_Query([
            'category__not_in' => [$news->term_id],
            'post__not_in' => [$post->ID],
            'posts_per_page' => 4,
            'post_status' => 'publish',
            'ignore_sticky_posts' => '1'
    ]);

I hope it will help somebody.

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.