DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Introduced in WordPress 2.7, stciky posts are an awesome new functionality, but sadly, retrieving and sorting them isn’t easy as you can expect. In this recipe, you’ll learn how to easily retrieve sticky posts.

To achieve this recipe, place the following code just before the loop:

	$sticky = get_option('sticky_posts');
	rsort( $sticky );
	$sticky = array_slice( $sticky, 0, 5);
        query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );

This code will retrieve the 5 most recent sticky posts. To change the number of retrieved posts, just change the 5 by the desired value on line 4.

Credits goes to Justin Tadlock for this awesome recipe!


  1. Have you got a way of inserting them at a certain position in the loop as well? I’m currently using all sorts of different methods to retrieve posts, managed to get posts and insert the stickies into their correct position. However, when I got to display them, for example as on the homepage of http://www.apeldoorndirect.nl, where I might want a single sticky post displayed as the third post (first smallest type post), the_loop changes the sorting applied when getting the posts from the database and stubbornly sticks it in first position, defying the sorting (applied first with merging ID arrays, followed by using that in an SQL query, turning on ‘&ignore_sticky_posts=1’ (so normal position applies)). Can you help?

  2. Nikita Mishra

    great tip..

  3. thanks but this query eat much more RAM of my server.

Leave a Comment

Your email address will not be published. Required fields are marked *