Definitive Sticky Posts Guide for WordPress 2.7

As the blogosphere is abuzz about all the cool new features in WordPress 2.7, I’ve seen very few (if any) good posts on the new Sticky Posts feature that will allow you to take any story, or stories, you’ve published and place them at the top of your homepage without editing the timestamp.  This new feature will allow you to take posts that you’re especially proud of and display them for all the world to see for as long as you want.

But for many theme and plugin developers, and even users who may want to do a little DIY theme hacking, there is very little documentation for getting the feature set up to do the things you want it to.  That’s where this guide comes in.

Turing a Post into a Sticky Post

picture-1In the 2.7 Post edit panel, over in the publish module, you’ll notice a little checkbox.  When you check this box and publish the post (or save an already published post), that post will then be pulled to the top of your homepage.

Also, it is worth noting that a post can be made “sticky” using the “Quick Edit” panel as well. In WordPress 2.7, simple click the Posts -> Edit link, then click the “Quick Edit” link.

This will bring up a panel that lets you edit common features of a post like its categories, tags, title, slug, date, author, etc.  But it also allows you to change whether or not a post is a sticky post.

picture-2

This is especially handy when you need to “unsticky” a post in order to let another post take it’s place at the top of the homepage.  Imagine how tedious it would be to have to pull up the full Edit Post panel just to “unsticky” a post.  It would get very annoying, so I’m glad to see the developers opted to include this option in the Quick Edit panel as well.

Styling a Sticky Post

If you’re like me, you probably want to have a way to let your readers know that, although the post is at the top of your homepage, it may not necessarily be the latest post you’ve published. Luckily, WordPress 2.7 introduces the new post_class() template tag that will give a class of “sticky” to any post that has been made into a sticky post.  Here’s how you would use it.

(By the way, it would be worth your while to take a look at the Codex page for post_class when it becomes available. The function can really give you some cool flexibility in how different posts are styled. I suppose that’s another post for another day.)

If you’re familiar with the loop, you’ll probably recognize the following code (or some variation of it):

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content(); ?>
<?php endwhile; endif; ?>

In order to use the new post_class() template tag, just be sure to wrap the content of each post that the loop outputs in a new div.  The code would looks something like this:

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div <?php post_class(); ?>>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>

Depending on what type of post it is, it will output different kinds of stuff.  For instance, if you view the source code for my homepage, you’ll see my posts wrapped in these divs already.  For the post Community, Self-Respect, and Free WordPress Themes, the div class generator created this:

<div class="post hentry category-blog category-general category-planet-wordpress category-wordpress-news tag-elevate-themes tag-free-wordpress-theme tag-free-wordpress-themes tag-wordpress">

Now, if that post had been marked “sticky”, then you would have seen the word “sticky” show up in that list too.  You can use any of those classes to style a post differently, but in this case, we’re just looking to differentiate the sticky posts. By adding a new class declaration in our CSS file, we can make a sticky post look any way we want:

.sticky {
    background: white;
    border: 2px solid black;
}

The possibilities are really endless — it’s all up to you!

Sticky Styling in Other Templates

As far as I know, the sticky feature does not actually affect any other templates besides the homepage post listing.  The only possible exception would be the search results (see the Trac ticket for more information).

However, 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 browsing through someone’s archives, and you are using the post_class() template tag, your sticky posts will get that sticky styling, just as though they were at the top of the homepage.

To remedy this, you’ll want to figure out a way to only apply that special sticky style to sticky posts that are on the homepage.  That means we’ll want to exclude things like category and date archives, page 2 of the blog homepage (example), and any other templates that you might use (tags, single posts, etc.).

In order to accomplish this, you’ll need to figure out a way to add a class or ID to the body tag of your theme.  Darren Hoyt has a good post on how to do that is pretty good, but I’ll try to write up a post later that will teach you how to mimic the post_class function for your body tag.

So, once your body has a class or ID, just modify your CSS a little to reflect your desire for the sticky style to only be applied to the homepage:

#home .sticky {
    background: white;
    border: 2px solid black;
}

Easy enough?

is_sticky() Conditional Tag

Along with the ability to use the post_class() to identifiy sticky posts, you can also use the is_sticky() conditional tag to check to see if a post is sticky or not.  Here’s a practical example (to be used within the loop):

<?php
if (is_sticky()) echo 'This is a sticky post!';
?>

The is_sticky() conditional tag returns a Boolean value of TRUE or FALSE depending on whether or not the post is a sticky post.  Use your imagination to determine the different applications this tag can help you achieve.

A Custom Loop to Return All Sticky Posts

If you’re a hardcore theme developer and want to use a custom loop to return only posts that have been marked as sticky, you can use this very handy code provided by Otto at the Support forums:

query_posts(array('post__in'=>get_option('sticky_posts')));

This code goes before the loop. It qualifies the loop by telling it to only return posts that have been marked “sticky”.  You can use all the other parameters of the query_posts() loop qualifier as well, but that post__in part is the part that actually tells the loop to only return the sticky posts.

A Custom Loop to Ignore Sticky Posts

Generally speaking, sticky posts will show up at the top of ever single loop you use on your homepage.  So for complex themes that use multiple loops on the homepage to return different data, each and every one of those loops will return the same sticky post(s).  And it seems like even loop qualifiers like the query_posts() argument post__not_in don’t work on excluding posts.  No matter what you do, those sticky posts will be at the top of all your loops.

Thankfully, once again Otto comes to the rescue with some more handy code.

If you want your loop to ignore the sticky status of posts, just use this code before the loop:

query_posts('caller_get_posts=1');

Again, this parameter can be used with any of the other arguments for query_posts()And this code doesn’t simply exclude posts marked sticky … it actually ignores the sticky status altogether.  So if a person marks a post as sticky, by using this query_posts() parameter, you can have your loop act normally, not pulling that post to the top unless it is the latest post published (depending on the other query_posts parameters).

Getting Rid of Stickies Completely

Finally, if your theme doesn’t take advantage of the sticky option at all, and you just want to prohibit users from making a post sticky in the first place, you can just wipe out the field in the options table that holds all the sticky post IDs by placing this code in your theme’s functions.php file between <?php ?> tags: [HT: WPEngineer.com]

update_option('sticky_posts', array());

This empties the field and get’s rid of all sticky post IDs that the user may have saved.  This could easily save you the trouble of having to support users that insist on checking the sticky checkbox despite your warnings.  It’s just a way for you to nip the problem in the bud.

However, use it with caution.  Users don’t normally like it when you remove their data.

Well, that about wraps it up.  Everything you need to know about the Sticky Post feature in WordPress 2.7. If you have any other tips or questions, please feel free to let me know in the comments below.  I’d love to hear your thoughts about this new feature.

Comments

    • Nathan Rice says

      When I did the initial research for Sticky Posts, that’s exactly what I thought too. Such complexities in such a simple option.

      But when you work with as complex themes as I do, you HAVE to think about these kinds of things :-)

  1. erica says

    I love the sticky feature. Before this, I used a Sticky Plugin for years.

    But, with this feature, on different themes, I have run into a problem.

    I am trying to figure out how to keep 10 posts on the front page and have (for example) 4 of them be sticky.

    What I have found is if I set the posts to display 10 per page (what I want) I enter 10 per page to show.

    BUT if I make 4 sticky on the front page – it ADDS 4 to the front page and also adds 10 to the front page besides the 4 stickies. I dont want 10 on the front page PLUS 4 sticky, I want to make 10 per page with 4 on the front page sticky , 6 non-sticky and then have the other pages have 10 on them.

    In other words if I make any sticky, not to add it to the front page plus the number per page, but make the sticky on the front part of the number per page.

    I hope this makes some sense. The only way I have found around this is make the “sticky” ones non sticky and change the posting date on them (which I do not want to change)

    • Nathan Rice says

      I pleaded with the developers on the mailing list to make the sticky posts NOT add to the total number of posts per page, but was politely told that the effort to change the way it is was not worth it.

      So, we’re forced to use workarounds.

      In your case the best thing to to would be to use a custom loop. If that sounds foreign to you, then the rest of this probably won’t make sense either, so it may be better to find a contract developer to do this for you.

      You’ll want to either use query_posts or WP_query to qualify your loop. The paramenters you’ll want to make sure you include are the “showposts” and “paged”.

      Pull the number of posts you’ve chosen to display per page from the options table.
      Pull the array of sticky post IDs from the options table and get a total number by using the “count” function.

      The final code would look something like this (roughly):

      <?php
      $num = intval(get_option('posts_per_page')) - intval(count(get_option('sticky_posts')));
      query_posts('showposts='.$num.'&paged='.$paged);
      ?>
      

      (sorry if the code doesn’t work perfectly. I’m writing this almost completely from memory. It may need tweaking.)

      Hope that helps, or at least points you in the right direction.

  2. erica says

    Nathan, Thank you for such a quick response! Your whole post and your response as well is excellent!

    For most of my sites (6 or 7 at present) I have no problem with it and love the sticky post feature. For the hard-to-figure one, I did find a temp work around, but I was not sure this would be the correct place to mention it.

    http://www.ericastjohn.com/ is the site where I was having the issue and I wanted the front page to be sticky. So, I ended up using a plugin called Opt-in Front Page which let me choose which posts are on the front page. The only problem with that is the paging is not at the bottom since it does not ‘continue’ as would normally if posts were chronological and continued on the next page.

    From your response, I think I will try to work a way using the sticky and your work around when I have a chance tomorrow to work on it more in depth (long day at work today)

    Again, thank you for such a generous and quick response. I hope I did not detract from your brilliant post with all the options on the sticky with my (problem) comment. I learned quite a bit from your post and your response. I am glad you understand what I meant and I hope your response and post will help many others as much as it has helped me! PS feel free to delete my comment if it is too long ;)

    • Nathan Rice says

      @erica,
      It’s really no problem at all. In fact, your question is really something that I should have addressed in my post. I’m considering writing a followup just to add the workaround in. I can see how this would be a common problem for some people. They should know the solution, even if it is a little complicated.

      Nathan

  3. falconaire says

    falconaire said at
    http://wordpress.org/support/topic/155585

    I want one of my posts to show up at the top of a category, but not always at the top of my index page.

    I searched for a plugin and found Wp Sticky but I don’t think that will work for what I’m trying to do, since I only want it at the top of the category.

    If you don’t know what I mean go to my site http://falconaire.designteen.com and click on Sports. I want the post “Matt’s Sports Blog” to be at the top of that page, but not at the top of the main page that shows the 5 most recent posts.

    Reply

  4. Hirvine says

    Nice nice, “Styling a Sticky Post” was very interested. I already checked whether my theme is compatible with WP 2.7. However it did not occur to me I needed to give the sticky post an alternate style (as it should I think).

    Now I get the solution on plate, thanks dude.

  5. Arun Basil Lal says

    Hey, I just noticed. You have not updated your footer for an whole year. it says All Content © 2007 Nathan Rice. Change it to 2009 now. you are skipping an year. lol ;)

  6. Chip says

    First of all, check your first subheader. it says TURING instead of TURNING.

    Second of all, I think it’s a great idea WordPress incorporates some of the most used hacks and/or plugins. Such as Sticky Posts. Such as counting words, which I’ve been using (as a plugin before 2.6.5) to count my ReviewMe posts.

    And, finally, I’ve upgraded my themes to be 2.7 compatible. I think all theme creators should do these, or should have done this sine the first Betas appeared.

  7. Tom Guile says

    Thanks for the guide, same like erica, I also used a Sticky Plugin for years and your way works perfectly on mine :D, great articles :)

  8. JeepnDave says

    Thanks for the sticky post guide Nathan, I have been using wordpress 2.7 since it was made available and I had no idea the sticky post was a built in feature now!

  9. ross says

    Thanks for your comprehensive guidelines.
    Just upgraded to WordPress 2.7 but unable to find ‘sticky’ checkbox except in quick edit mode.
    Please advise
    Thanks

  10. Av?a says

    Nice nice, “Styling a Sticky Post” was very interested. I already checked whether my theme is compatible with WP 2.7. However it did not occur to me I needed to give the sticky post an alternate style (as it should I think).

    Now I get the solution on plate, thanks dude.

  11. Ian says

    Nathan – Thank You :-) I was getting very frustrated by this until you pointed out a simple tick box! I still have a lot to learn! It only seems to be in the quick edit box now but works well, and goes exactly what I need.

  12. Brian says

    Thanks for the fine website.

    Is there such a thing as a “Sticky” Comment for a static page in WP2.7?

    I would like the management types who need to use this page to update volunteer staff to be able to make an announcement on a predictable static page and have it remain at the top in order to guide the volunteers (who also have the opportunity to add their remarks to the page)
    thanks
    Brian

  13. Brian says

    I should probably have mentioned in my prior post that my wp2.7 is privately hosted, not on wordpress.com
    Brian

  14. sergio says

    I just installed bbpress and for some reason all of my sticky notes and scheduling capabilities dissappeared.

    Thus everything gets published weather scheduled or not and sticky notes do not get respected.

    Does anyone have any idea what may have gone wrong ?

    Thanks,

    Sergio Sedas

  15. leslie says

    Hi Nathan,

    Great site and great post. I’ve seen several other comments with my same problem but haven’t found a solution on your site or other sites.

    I need to make a post sticky to its category only and not have it stick to the home page where I only want to display the most recent posts. When I select sticky to a post, it shows up also on the home page.

    Any suggestions?

    thanks,
    Leslie

  16. Kathy says

    Hi, this is a great tutorial.
    However, there is some parts I don’t get, most probably because I’m a newbie to this php stuff.

    1st–the code for ignoring sticky post–
    can I use this on my category template?
    I don’t want the sticky posts to show up on my category template, just on my homepage.

    2nd–since I am a newbie, I have no idea where to place this code? You said before the loop -which I know is the “if have post…while have post..etc.” but What do I do? do I just paste it above that code? Do I have to add php tags around it?

    I’d greatly appreciate your help.

  17. Wolfgang says

    Nice Post!
    Im new to WP, and only started diging my teath into it because a customer wants a homepage setup with wp instead of typo3… this sticky post guide helps a lot for that…

    cheers!!!

  18. Wolfgang says

    This worked beautifly when having only one sticky post…
    query_posts(array(‘post__in’=>get_option(‘sticky_posts’)));
    Brought out the typical homepage “start page” look…

    now Ill just add one post / category and she’ll be right…

    thanks for the short tutorial…

  19. SirChops says

    Thanks for the clear guidance, Nathan.

    I noticed people are having issues with sticking a post to a category archive. I have tested this and confirmed it does not work.

    For example, I have a category entitled “Advanced”. I want a post to stick to the top of the category archive when someone selects the “Advanced” category from my front page. (It could be a post as a general disclaimer about mastering early skills before tackling these advanced skills.)

    I am using a static front page, but I think some of these people are not but still would like just one category archive post to stick. I would like to know if there is a workaround to this as well because the functionality is not applicable if you are using a static front page.

    Thanks.

  20. lee jones says

    Thanks nathan, done a search on google for this topic and you were the first place i came to, and answered all the questions i had – so thanks again

  21. Redbourn says

    Two of my sites that had sticky pages, but when I later made them non-sticky they no longer load.

    If I make them stick again then they load.

    Any ideas what the problem is, or a fix?

    thanks,

    Michael

  22. David Ziet says

    Would it not create less verbose code to use:

    <div class="<?php if (is_sticky()) echo 'sticky';?>">

  23. David Ziet says

    Thanks a million! Based on your script, I wrote this <div class="<?php if ((is_home()) AND (is_sticky())) echo 'sticky';?>"> to restyle a post for the homepage but nowhere else in the blog.

  24. Drew says

    Great article, Nathan. In case you are still responding to comments on this post, I thought I might pose a question here that I haven’t found a response to elsewhere…Is it possible to sticky/feature a post on a category or tag page only? For instance, if one of my tags is “Cats” and I want one or two of my Cats tagged articles to come up first and perhaps be styled differently on the “posts tagged with Cats” page, do you have any suggestions for how to go about that? Is it even doable? Thanks so much!

  25. Florian says

    hi Nathan, good post.

    MY QUESTION:
    I want to organize the order of display of the 2 or 3 sticky posts I have. However, I tried to change the publishing dates of the posts, but the order does not change. Although my permalinks are changed to %postname%, I assume WP uses some kind of postnumber to assign the priority of the sticky posts.

    Do you know how I can control the order of display of several sticky posts?

    thanks in advance

  26. Fas says

    Hi Nathan,

    I wrote this code to show Sticky Post and if there is none, it just show the latest post.

    I have to just show one post on the page (either sticky or latest). To achieve it, I wrote the following code:-

    The problem is that It is only showing any sticky post. It does not show me latest post when there is no sticky post.

    I would be very thankful, if you can pick the problem or if you want, you can propose a better code! …


    $recentPosts = new WP_Query();

    $sticky = get_option('sticky_posts');

    $args = array(
    'showposts' => 1,
    'post__in' => $sticky,
    'caller_get_posts' => 1,
    'orderby' => 'date',
    'category_name' => 'ewp-news'
    );

    $recentPosts->query($args);

    while ($recentPosts->have_posts()) : $recentPosts->the_post();

    // The first one is always a Sticky, so if there is a sticky, it will check it and let it pass!
    if($sticky[0]) {
    // insert here your stuff...

    // Title and Excerpt here !

    ?>
    <?php endwhile; ?>

  27. Ken Shim, RMT says

    Thanks. Helpful stuff. I’m still trying to figure out how to have multiple stickies where the older sticky is shown as the feature article. I might have to play with the publication date.

  28. Cracks says

    Great info feed Nathan; thanks.

    Saw the loop you pubished to return all sticky posts. Is it possible to output just a list of the sticky post titles (basically using Sticky as Featured Posts, and want a list of links to those sticky posts) ..?

  29. Kasia says

    Hey Nathan thanks for the info, very useful.
    I am having trouble with some stickies, not sure if you can help.
    On my home page I have 4 loops. Main loop of all articles, then 3 separate loops in a side column that show stickies for 3 different categories. Problem is some stickies don’t appear. So category1 may have 3 stickies but only shows 2.
    I thought maybe it’s because the posts not appearing are part of several categories but so are the other stickies and they show up.
    Any ideas?

    Thanks.

  30. schalk says

    I created a second “blog page” by duplicating my theme’s index.php and called it mill.php. I then directed certain categories to be posted to my mill.php
    I wanted to add a sticky, but even though the sticky post is only in the mill category, it shows up on the main page too?

    Any help how to prevent it?

    Alternatively, How can I ad my sticky post (text and image) in the mill.php code? I will need the text to be certain colors and size too.

    thank you