I’m using pre_get_posts
to adjust the number of posts displayed on my homepage.
function lifelounge_query_adjust( $query ) {
if ( is_home() ) {
set_query_var( 'posts_per_page', 12 );
return;
}
}
add_filter( 'pre_get_posts', 'lifelounge_query_adjust' );
But I am running into a problem with sticky posts. Basically, if I have any sticky posts, the query will display more than the 12 posts I have specified, because it will display 12 plus any sticky posts. I could, of course, ignore sticky posts:
function lifelounge_query_adjust( $query ) {
if ( is_home() ) {
set_query_var( 'posts_per_page', 1 );
set_query_var( 'ignore_sticky_posts', 1 );
return;
}
}
add_filter( 'pre_get_posts', 'lifelounge_query_adjust' );
But I don’t think this is ideal. I think the sticky posts should be included in the limit of 12 posts, and not added to the limit. That is what makes the most sense to me. Is there a way to achieve that? Have I made a face-palm-worthy error?
Pretty much a duplicate of: Sticky Posts & Posts Per Page but that was weirdly closed as too localized. I disagree, obviously because I’m looking for an answer, but also because it is a question of why WordPress doesn’t seem to respect the posts_per_page
limit if you are using sticky posts. If you want 12 posts per page you should get 12, not 13, which is what you would get if you had a single sticky post.
Here is an approach to account for sticky posts by getting the number of sticky posts (if any) and include that in the calculation
posts_per_page
parameter:Edit
In the case where the number of posts per page we wish to set is less than or equal to the number of sticky posts, I have set the
posts_per_page
to one and that will result in 13 or more posts$sticky_count + 1
(in this case) only on the first page (subsequent pages will have 12 posts). Maybe that is OK since this case is rare and +1 post on the first page may not be that significant.This is because WordPress will display all sticky posts first and on one page (the first page) even if their count is greater than the
posts_per_page
parameter, so we set theposts_per_page
in this case to the minimal amount possible which is1
, because0
and negative values will disable theposts_per_page
parameter and that will make WordPress to display all posts on the first page.There an issue if the sticky posts are in the first page.
The solution is to decrement the sticky post count for the sticky posts that are part of the first page.
I hope it will help
The answers written here get the job done, but WordPress provides a simpler way. As part of the
WP_Query
class, WordPress includes thecurrent_post
property. This property increases by one every time a new post is processed through the loop when you call$my_query->the_post()
. The property is set initially to -1.So, in order to ensure you always display the right number of posts, including sticky posts, you can set it up like this. In the example below, two blog posts will always be shown, no matter how many sticky posts there are.
This approach has only been tested on a custom query, but should work with the default WordPress loop as well.
I cleaned up both of the above answers into one so that it does not load needless WP_Query, fixes if the sticky on first page, reduce the time to process the information with cleaner faster code.