Get latest sticky posts of wordpress

April 11, 2009 · Print This Article

Stciky posts are an awesome new functionality of wordpress, but sadly, retrieving and sorting them isn’t easy as you can expect.

We’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 five latest sticky posts.

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

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.

Your choice for site templates and wordpress themes


Got something to say?

You must be logged in to post a comment.