Make posts_per_page value the same on all pages

The problem is that on your first page you ask for, lets say, 7 ‘normal’ posts. So it displays the latest 7 posts: e.g. 15-9.

On page two you are telling WordPress I want 8 posts per page and I want the second page. The internal logic is that if you have 8 posts per page and you are on the second page then you want the 9th to 16th posts (in this case the 9th post is 7 since its ordered by date).

You can manually set the offset to correct this, but it requires knowing how many ‘normal’ posts appeared on the first page. In any case…

Regarding having sticky posts at the top – WordPress should do this for you. That is, if you omit the post__not_in attribute then sticky posts should appear at the top, and you’ll have the same number of posts for each page. Unless this is for a page template – you shouldn’t event need to run your own query.

Regarding the code you’ve posted:

  • Avoid using the variable $wp_query – this is what WordPress calls the ‘main query’ for the page.
  • For checking if you are on the first or subsequent pages use is_paged()
  • It’s generally better to use the tax_query rather than ‘category_name’.
  • If you can, avoid editing the loop inside the template – instead use the pre_get_posts hook to make changes to the query before it gets there. See this post and this Codex.

Hope that helps 🙂

tech