I came across an interesting question the other day. A user wanted to know how to query a specific number of sticky posts for the front page. Generally, I just point people to the Definitive Sticky Posts Guide for WordPress, but this question was not covered.
The problem:
- User has many sticky posts (maybe 100s).
- User wants to only show a specific number of those ordered by date.
Some of you may be thinking that this should be extremely simple. But, let me warn you, I thought the same thing at first. I had to actually put on the ol’ thinking cap for this one.
Get the latest x number of sticky posts
I’ll assume you already have The Loop set up in one of your template files (i.e., home.php, custom-page.php). What we need to do is call up a specific number of stickies. For this example, we’ll load the two latest sticky posts.
Place this code before your Loop:
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the 2 newest stickies (change 2 for a different number) */
$sticky = array_slice( $sticky, 0, 2 );
/* Query sticky posts */
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
We have to use rsort() to sort the sticky posts in reverse order by post ID (newer posts first). The reason for this is because sticky posts are added to the sticky array according to when they were made sticky, not by ID.
We have to set caller_get_posts to 1. Otherwise, we’d get all stickies added.
The biggest problem here is that you have to add Updated: Hat tip to Nathan Rice for the $sticky[0], $sticky[1], $sticky[2], and so on. But, typing a few extra characters is a small price to pay for the functionality it offers.array_slice() solution.
Other solutions for sticky-post issues?
As more users start taking advantage of sticky posts, there’ll probably be more issues such as this one pop up. Have you run into any problems querying sticky posts in a certain way? If so, how did you figure it out?
I hope this tutorial at least helps some of you out when working with sticky posts. If you have a more elegant solution for the issue posted above, I’d love to see the code.

Just curious, I haven’t tested this, but using the “limit” parameter on query_posts won’t work? I assume that due to the trouble you went through to write this up, it didn’t work, but I wanted to check anyway.
Nathan Rice — I couldn’t get
limitto work. Even if it did, we’d still need to sort the sticky posts.The problem seems to come from using
post__inbecause we’re specifying the posts we want. I can’t find a way to limit it when specifying all the stickies.@Justin,
Yeah, that makes sense.
BTW, using array_slice on the stickies array will help you reduce some code. Instead of explicitly doing $sticky[0], $sticky[1], etc. you could just do:
$sticky = array_slice($sticky, 0, 3);3 of course would be the limit for the number of stickies you’d want to return with the loop.
Then you could just do this:
A little harder to follow, but much more efficient. You could probably even consolodate the first 2 lines.
I think this would be extremely nice to see in Hybrid.
When Hybrid News came out I started converting all my featured posts to sticky posts, but alas they just all started showing up in the slider. This could be a fantastic way to re-actualize the sticky post as a featured one.
Nathan Rice —
array_slice()was just what I was looking for. I can’t believe I didn’t think of that. It certainly makes things more efficient. Updating the post now.Thomas Clausen — Definitely. When I originally coded Hybrid News, I hadn’t planned on allowing sticky posts for the slider. It was a last minute addition and nothing like this had been tested against it.
If a blog has that many sticky posts perhaps using sticky posts is not the best solution. It might be easier to place the posts into a special category and then query the category to display them.
Just a thought.
LGR — Agreed. I think the main problem here is that users will make a post sticky but will forget to go back and un-sticky it later. So, they end up with way too many sticky posts.
The best way to help users manage them would for the sticky posts to show at the top of the Edit page in the WordPress admin. That way, users could easily find them.
When you need to show the latest post marked as sticky, you can simply change
rsort()forarray_reverse(). It will preserve the array, only reversing its order.Another thing: for the case you and Nathan solved, we don’t need the
rsort()/array_slice()thing. I think this can be done usingquery_posts:$sticky, 'caller_get_posts' => 1, 'orderby' => ID, 'showposts' => 1));
?>
Damn. Again:
$sticky = get_option( 'sticky_posts' );
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1, 'orderby' => ID, 'showposts' => 2 ) );
@eduardo,
If you look at the comments above, Justin said that the
showpostsparameter didn’t do what it was supposed to do.@Nathan,
Yeah, I know, but it did work for me, really. Worth a shot.
Hi-
Awesome tip. I’m trying to display the either the (1) latest sticky post, or, if there aren’t any, just the latest post. But the loop is plopping down the latest sticky post AND the latest post. As noted, the showposts option only seems to limit non-sticky posts. Any suggestions?
How can I specify which category I want to get the sticky posts from?
@two7s_clash: I was struggling with this also and found this solution in a thread started by Nathan. Basically, put everything between a conditional
iftag to check if there are actually any sticky posts available. So, in your case, you would do something along the lines of the following:This is problematic in that two loops will have to be declared (at least I think), but it’s the only way around the problem.
Cheers,
Philip
I confirm @Edouardo, showposts parameter work fine. Array solution seems to be cleaner.
Is it possible to list sticky posts in the sidebar?
This is working great thanks!
I do have a question though and I’m hoping you will respond to this Justin.
I need to loop 2 times through the same sticky query as I need to create first an unordered list of some custom field value from each stick post and then again to get the title and excerpt out of each. It works fine but the page loads too slow. I’d like to show you if possible by providing a link-would you mind giving me a hand in finding out why the results load so slow?
Thank you SO much – it worked like a charm
(WP 2.8.3)
I started converting all my featured posts to sticky posts, but alas they just all started showing up in the slider. This could be a fantastic way to re-actualize the sticky post as a featured one.
Here again, a sizeable body of research literature shows that improvement between treatment sessions is the rule rather than the exception, with the majority of clients in successful therapy experiencing significant symptomatic relief earlier versus later in the treatment process. ,
Anyone know how to add something after the sticky posts?
so if sticky, show after all stickys and before normal posts. if no sticky, show before normal posts?
How would you handle two separate sticky post loops? I want to have sticky posts from a specific category in one loop, and then all other sticky posts in another loop. (Two different sections of the home page.) I’ve tried unsuccessfully to do so. Any insights? Thanks!
Hello,
I’ll put the code in front of my loop as you said but now it only shows my “sticky post”, the other one have dissapeared. Does someone have an idea to solve this ?
Thanks in advance
I’m have been looking for a way to make the second post on my homepage sticky. Does anybody know how to do this?
I am using this as my way of fetching 2 recent posts from a category, how do i incorporate the sticky feature to this code?
Great code, thanks for your guys hard work.
Could you help me. I am using this code and it works. However, under the sticky posts, I want my normal blog posts to show. What would I have to add to the code for that to happen?
In other words, on my homepage, I want my 2 sticky posts AND then my other blog posts under it.
Thanks
Great little piece of code. My only issue is that when there aren’t any sticky posts, it lists all the posts instead. Any way around that? I’m assuming an if statement but can’t seem to get it to work?
Sticky Posts are really complicated, wow.
Wondering if anyone has managed to get stickies to be listed first in search results if they form part of the posts found by a specific search query?
Is there a way to do this with
get_posts()instead ofquery_posts()?I’m using the code below to display the most recent post in a specific category (id=4) but what I’d like to do is only show the most recent sticky one.
whoa I figured it out—answered my own question above about how to do it with
get_posts(). The code below show the most recent sticky post from category id=4. In this case it shows the thumbnail only but one can edit the html part as needed. I’m using it on a homepage to show the most recent sticky post in 3 categories.Hello, I want to know, How to separate Sticky posts than other posts on Homepage? Like, http://www.labnol.org, it shows the sticky post with thumbnail and other posts without thumbnail on homepage.
[...] 15. Get The Latest Sticky Posts [...]
I decided I wanted a bunch of sticky posts instead of a ‘sticky’ category because it seemed intuitively right but then got in a mess because my new stickified posts would appear in an undesired order, i.e. ascending ID:
** Even where orderby was set in query_posts() parameters **
(I don’t think all users experience this; I did.)
I needed to order my homepage posts by a custom ‘meta’ variable and ended up writing some SQL to do it, so it’s kind of irrelevant that they are sticky, except that the stickiness caused the problem. So this code turns out to be more about working with the wordpress database… anyway here it is:
Comments welcome! (I’m very amateur at all this.)
Jon
Like I said, an amateur. Maybe code will appear this time…
Hi, I have a simple solution to this problem. I was trying to get my sticky posts to show on a static page. I couldn’t figure it out, so I made a new category called ‘sticky’, and then i simply called the most recent posts from the category.
Anything i want sticky, I just list in that category…
Great code, but how can I achieve so there is only one sticky post on homepage (the latest one) and under it the rest of the posts