I’m querying to get a list of all posts, 20 posts_per_page, but the result shows all posts, but with 21 posts per page. If I change posts_per_page to 19, then 20 show. One post is sticky and shows up twice; not sure if that is causing the problem.
Code:
$allposts = array(
'post_type' => 'post',
'posts_per_page' => 20
);
$wp_query = new WP_Query($allposts); ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php foo_pagination(); ?>
<?php endwhile; ?>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Sticky posts do add to the post count rather than being included in it. You can alter your query to ignore sticky posts though.
$allposts = array(
'post_type' => 'post',
'posts_per_page' => 20,
'ignore_sticky_posts' => true
);
But you are also missing pagination parameters.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$allposts = array(
'post_type' => 'post',
'posts_per_page' => 20,
'ignore_sticky_posts' => true,
'paged' => $paged
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0