3

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?

2
  • try to change the posts per page to 3 from admin panel, and see what happens Commented Mar 3, 2015 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 Commented Mar 3, 2015 at 7:13

1 Answer 1

7

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;
          $q->set('posts_per_page',$ppp);
          $q->set('offset',$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 );     
5
  • I tried inserting this code in functions.php but it doesn't seem to be working. Still working on it, we must be close. Commented Mar 3, 2015 at 20:15
  • Seems you got it working at the end :-). Enjoy Commented Mar 4, 2015 at 3:16
  • 1
    ( $ppp - ( $ppp - $count_stickies ) ) is just $count_stickies. :)
    – webtoure
    Commented Aug 8, 2015 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
    Commented Jul 11, 2016 at 14:30
  • 8 years later: Attempted this approach, and I found that it breaks the pagination calculation for max_num_pages somewhere (maybe because a lot has changed in WP since this post). By having 1 sticky post, it adds one page to the max_num_pages count, providing a link to an empty (or 404) page. I tried to adjust it to no avail using the information from WP here: codex.wordpress.org/…, but it still just adds an extra page. When I set another post to "stick," it added 3 pages. Commented Oct 27, 2023 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.