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.
- I've tried remove all plugins: nothing happens
- I've tried to install my theme on empty blog: works correctly
- I've tried to remove all caches (object cache, wp_query cache): nothing happens
- I've tried to change
category__not_in
arg tocategory__in
: works correctly - Add
wp_reset_postdata();
before the loop: nothing happens
So I can't understand why it works this way. Any help will be appreciated