Definitive Sticky Posts Guide for WordPress 2.7

November 11th, 2008 Nathan Rice

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-1
In 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.

Similar Posts:

The Tags: , , , ,

37 Responses to “Definitive Sticky Posts Guide for WordPress 2.7”

Spencer Said:

Nice! Thanks, Nathan.

Comment made on November 13th, 2008 at 6:30 pm
Jeffro2pt0 Said:

Damn man, who would of thought that the Sticky Post option would provide so much to write about? Great guide Nathan.

Comment made on November 14th, 2008 at 8:34 pm
Nathan Rice Said:

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 :-)

Comment made on November 14th, 2008 at 9:01 pm
panjur Said:

Thanks man.

Comment made on November 15th, 2008 at 8:01 pm
erica Said:

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)

Comment made on November 15th, 2008 at 11:20 pm
Nathan Rice Said:

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.

Comment made on November 15th, 2008 at 11:37 pm
erica Said:

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 ;)

Comment made on November 15th, 2008 at 11:45 pm
Nathan Rice Said:

@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

Comment made on November 15th, 2008 at 11:51 pm
yigerensh Said:

need sticky post per cat. or tag.
but they wont all come to the top of front page

any idea ?

Comment made on November 19th, 2008 at 5:05 am
falconaire Said:

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

Comment made on November 19th, 2008 at 5:07 am
Hirvine Said:

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.

Comment made on November 28th, 2008 at 6:48 am
Arun Basil Lal Said:

Hey, Thanks for the hack, I have made a post on when to use sticky post effectively (http://www.millionclues.com/blogosphere/blogging-tips-blogosphere/using-sticky-posts-wordpress) and was digging it and found this. I am on your twitter too.

Great job,
Arun

Comment made on December 15th, 2008 at 4:31 am
Arun Basil Lal Said:

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 ;)

Comment made on December 15th, 2008 at 4:31 am
Hirvine Said:

If you bother updating the year put
instead.

Comment made on December 15th, 2008 at 3:56 pm
Nathan Rice Said:

@Hirvine @Arun
I probably won’t bother to fix it now. A redesign is on the way soon.

Comment made on December 15th, 2008 at 4:04 pm
Hirvine Said:

Yeah well, it wouldn’t bother me, but since @Arun Basil Lal mentioned it, I couldn’t help it. … new design ^^.

Comment made on December 15th, 2008 at 4:06 pm
Chip Said:

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.

Comment made on December 21st, 2008 at 4:11 pm
Blog Traffic Excahnge Said:

I wrote a plugin that solves 99% of the sticky needs when you are not using date based permalinks. It also reinjects the post into the rss feed. Unfortunately it does require editing the timestamp…

This feature will need to be explored further to determine how, when, why, an if it should be used by the Old Post Promoter…
http://www.blogtrafficexchange.com/old-post-promoter/

Comment made on December 27th, 2008 at 11:49 am

Trackbacks

sympozium » Blog Archive » Sticky Posts Guide for WordPress 2.7 Said:

[...] 2.7 are on their way, we have to be aware of them in order to use them immediately! So here is a Sticky posts guide from Nathan Rice Share this [...]

Comment made on November 14th, 2008 at 4:24 am
WordPress - An Icon Of Its Time | Weblog Tools Collection Said:

[...] Guide To Sticky Posts [...]

Comment made on November 15th, 2008 at 2:23 am
WordPress - An Icon Of Its Time | BlogBroker24-7 Said:

[...] Guide To Sticky Posts [...]

Comment made on November 15th, 2008 at 3:10 am
WordPress News for 11/15 | Weblog Tools Collection Said:

[...] Rice posted a Definitive guide to Sticky posts in WordPress 2.7 complete with screenshots, style suggestions, code examples and custom loop suggestions. He even [...]

Comment made on November 15th, 2008 at 8:21 pm
Asides: WordPress Sticky Posts :: Cory Miller | Entrepreneurship, Marketing and WordPress Said:

[...] explains the upcoming WordPress 2.7 Sticky Posts Filed in: Asides • Saturday, November 15th, [...]

Comment made on November 15th, 2008 at 8:40 pm
WordPress News for 11/15 | BlogBroker24-7 Said:

[...] Rice posted a Definitive guide to Sticky posts in WordPress 2.7 complete with screenshots, style suggestions, code examples and custom loop suggestions. He even [...]

Comment made on November 15th, 2008 at 9:16 pm
WordPress News for 11/15 | Wordpress Blog NL Said:

[...] Rice posted a Definitive guide to Sticky posts in WordPress 2.7 complete with screenshots, style suggestions, code examples and custom loop suggestions. He even [...]

Comment made on November 16th, 2008 at 2:19 am
Sticky Posts ? WordPress 2.7 Said:

[...] ?? Sticky Posts ? WordPress 2.7 Definitive Sticky Posts Guide for WordPress 2.7 :: Nathan Rice Wordpress Posts in Different ColumnsI have seen many people online asking how to divide blog posts [...]

Comment made on November 17th, 2008 at 3:54 am
New Page Menu Function & Sticky Posts in WordPress 2.7 Said:

[...] the meantime, Nathan Rice has published the Definitive Guide to Sticky Posts in WordPress 2.7.  This is good information to have, as WordPress 2.7 will include a new “sticky post” [...]

Comment made on November 17th, 2008 at 4:01 am
links for 2008-11-17 | links | WereWP Said:

[...] Definitive Sticky Posts Guide for WordPress 2.7 :: Nathan Rice (tags: WordPress 2.7 tutorial themes sticky) [...]

Comment made on November 17th, 2008 at 10:06 am
Coderies | taggle.org Said:

[...] Les sticky posts sur WordPress 2.7 [...]

Comment made on November 18th, 2008 at 4:55 am
10 Things You Need To Know About WordPress 2.7 | Technosailor.com Said:

[...] Nathan Rice has a fantastic write-up on sticky posts in WordPress 2.7. [...]

Comment made on November 18th, 2008 at 10:41 am
New Page Menu Function & Sticky Posts in WordPress 2.7 Said:

[...] the same time, Nathan Rice has published the Definitive Guide to Sticky Posts in WordPress 2.7.  This is good information to have, as WordPress 2.7 will include a new “sticky post” [...]

Comment made on November 21st, 2008 at 9:39 am
WordPress Wednesday News: | The Blog Herald Said:

[...] Nathan Rice - Definitive guide to Sticky posts in WordPress 2.7 [...]

Comment made on November 22nd, 2008 at 2:40 am
WordPress 2.7 Release News and Links « Lorelle on WordPress Said:

[...] Nathan Rice - Definitive guide to Sticky posts in WordPress 2.7 [...]

Comment made on November 28th, 2008 at 2:14 am
WP 2.7 Blog Party is ON! | Human3rror - Where Typos are Part of the Equation Said:

[...] Nathan Rice - Definitive guide to Sticky posts in WordPress 2.7 [...]

Comment made on December 11th, 2008 at 3:10 pm
Top WordPress 2.7 Tips, Hacks, Plugins & Resources | ShanKri-la Said:

[...] Definitive Sticky Posts Guide Sticky posts is a new feature in 2.7. Nathan Rice has an excellent guide on how to use, create and style sticky posts! [...]

Comment made on December 11th, 2008 at 5:02 pm
 

Leave a Comment