How to: Get latest sticky posts

April 10, 2009 at 9:30 am

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!