I would like 7 posts showing on my homepage. I would like to be able to manually choose those posts by stickying them.

Currently, I have my posts per page set to '7' in WP Admin. However, this does not include sticky posts as I now am having 7 sticky posts + 7 newest published posts displaying for a total of 14.

How do I include the sticky posts in the posts per page count?

  • try to change the posts per page to 3 from admin panel, and see what happens – Abhisek Malakar Mar 3 '15 at 7:05
  • 1
    This is a nice one. Unfortunately I cannot code now, but you are going to have to count your sticky posts, subtract that from your posts per page option and then make use of an offset to recalculate the posts on your 1st page. You will also need to recalculate found_ posts to adjust for this change otherwise pagination fails on the last page – Pieter Goosen Mar 3 '15 at 7:13

This is doable, and as I stated in a comment, you need to follow the following steps

  • Get a count the amount of sticky posts. This can be done by simply counting get_option( 'sticky_posts' ) which holds the ID's of the sticky posts in the form of an array

  • You would also need to get the amount of posts per page. You don't want to hardcode here. The amount of posts per page is stored in get_option( 'posts_per_page' ), so you are going to use this

  • The important part now is to make use of offsets to recalculate the amount of posts to show on page one from the main query. As per example in the OP, if posts per page is set to 7, and there are 4 sticky posts, we only want three posts from the main query, and not the seven set as the site's default. From page 2 we will still need the normal 7 posts to show from the main query as we will not have sticky posts here

  • This offset will however influence the calculation of the $max_num_pages property in the main query, and what that means is, in most cases the last page will be missing. To correct this, we need to adjust the amount of posts found, basically adding our offset to the actual amount of posts found in order to correct the amount of pages. This will be done through the found_posts filter

(NOTE: This code is now tested and working)

Here is the basic idea in code:

add_action( 'pre_get_posts', function ( $q ) 

    if ( $q->is_main_query() && $q->is_home() ) {

        $count_stickies = count( get_option( 'sticky_posts' ) );
        $ppp = get_option( 'posts_per_page' );
        $offset = ( $count_stickies <= $ppp ) ? ( $ppp - ( $ppp - $count_stickies ) ) : $ppp;

        if (!$q->is_paged()) {
          $q->set('posts_per_page', ( $ppp - $offset ));
        } else {
          $offset = ( ($q->query_vars['paged']-1) * $ppp ) - $offset;



add_filter( 'found_posts', function ( $found_posts, $q ) 

    if( $q->is_main_query() && $q->is_home() ) {

        $count_stickies = count( get_option( 'sticky_posts' ) );
        $ppp = get_option( 'posts_per_page' );
        $offset = ( $count_stickies <= $ppp ) ? ( $ppp - ( $ppp - $count_stickies ) ) : $ppp;        

        $found_posts = $found_posts + $offset;
    return $found_posts;

}, 10, 2 );     
  • I tried inserting this code in functions.php but it doesn't seem to be working. Still working on it, we must be close. – Travis Patron Mar 3 '15 at 20:15
  • Seems you got it working at the end :-). Enjoy – Pieter Goosen Mar 4 '15 at 3:16
  • ( $ppp - ( $ppp - $count_stickies ) ) is just $count_stickies. :) – webtoure Aug 8 '15 at 20:23
  • I just tried this out. With two or more sticky posts it does not work. It only works if I only have one sticky post. Any way to fix this? – Arete Jul 11 '16 at 14:30

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.