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.

3 Comments
  1. LaShelle says:

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

  2. 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 says:

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

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

Would you like to advertise here? Click here for more information.

Recent Posts
Recent Comments
Categories
Most Popular Posts
Archive