Strange Things With Sticky Posts

Frank wrote in one of his last posts about the new sticky functionality in WordPress 2.7. Right now I’m working on a special theme and I noticed some strange things with sticky posts.

Wrong Post Count

If you like to show 6 posts on a page, I use query_posts('showposts=6');. But if one or more posts are “sticky”, they get added to the other 6 posts. To show 6 posts actually, you have to know how many sticky posts you have.

The solution:

get_option('sticky_post') gives you an array back with sticky post ID, which we can count.

$sticky = count(get_option('sticky_posts')); 
query_posts('showposts='. ( 6 - $sticky ));

Still showing sticky posts in chronological sort order

Pay attention to the CSS classes of the posts. The template tag post_class() gives the post the class. In the case of sticky post, it is also the class “sticky”. If you think you can give the class sticky a red background and black border to have a nice teaser you will be disappointed. If you go to the next page to see previous posts, the sticky post will also be showed in the chronological order with a red background and black borders. Not really cool.

Kill the Sticky

If you don’t want to have sticky posts, just write in your template:

update_option('sticky_posts', array());

With the empty array you overwrite the sticky options and no sticky posts are availably.


Posted

in

by

Comments

9 responses to “Strange Things With Sticky Posts”

  1. […] Strange Things With Sticky Posts at WPEngineer.com […]

  2. […] there is one caveat as pointed out by Michael at WPEngineer.com — sticky posts don’t simply get “pulled to the front of the line”.  In […]

  3. LaShelle Avatar
    LaShelle

    Thank you! Just what I needed and worked like a charm!

  4. Andreas Karlsson Avatar

    What about making two querys? One containing only the sticky posts and another using caller_get_posts=1 to get your posts excluding the sticky ones.

    Post & Page Parameters

  5. Michael Avatar

    Yes Andreas. That cller_get_post was imolemented after this post. The wrong count is fixed with the release of 2.7 too

  6. […] there is one caveat as pointed out by Michael at WPEngineer.com — sticky posts don’t simply get “pulled to the front of the line”.  In fact, if you’re […]

  7. Audrius Avatar

    Well it works only with first page. for ex. i have one sticky post and i have 99 more posts and i want show them by 20 posts per page. So i get that in first page i got 20 posts, but in all others i got 19 (20-$sticky).

    Anybody know how to solve it in all pages?>

  8. Michael Avatar

    @Audrius: try that one:

    $sticky = count(get_option('sticky_posts'));
    if(is_paged()){
    $sticky = 0;
    }
    query_posts('showposts='. ( 6 - $sticky ));

  9. Audrius Avatar

    at first sight it was OK, but …
    with first page everything is OK, but second page misses one post (the first one in second page) but total number is correct. I think it is because that sticky post also is shown in second page but not in first position (in the place where it should be as common post)