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.

Comments are closed.


  1. LaShelle

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

  2. Andreas Karlsson

    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

  3. Michael

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

  4. Audrius

    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?>

  5. Michael

    @Audrius: try that one:

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

  6. Audrius

    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)

3 pingbacks

  1. Feed Subscriptions, Stats and Management « Feet up, eyes closed, head back
  2. Definitive Sticky Posts Guide for WordPress 2.7 |
  3. Xtremegang » Blog Archive » Sticky Posts Guide for WordPress 2.7