Wordpress

How to Display the Latest Sticky Posts in WordPress

WordPress has this very cool feature called stick posts. When you make a post sticky, it shows up above your new posts, but only if your theme is set to be that way. In this tutorial we will show you a trick that will let you display latest sticky posts anywhere in your WordPress theme.

Assuming that you have already created a custom page template and/or already have The Loop ready, paste the following code before the loop.

01 <?php
02 /* Get all sticky posts */
03 $sticky = get_option( 'sticky_posts' );
04
05 /* Sort the stickies with the newest ones at the top */
06 rsort( $sticky );
07
08 /* Get the 5 newest stickies (change 5 for a different number) */
09 $sticky = array_slice( $sticky, 0, 5 );
10
11 /* Query sticky posts */
12 query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
13 ?>

This code can very well be used in featured slider, or any other advanced feature that you would like to display on your site. This snippet is mostly geared toward a WordPress site that has a custom homepage or a magazine style look.

The credit to this code goes to Justin Tadlock and partially to Nathan Rice for coming up with the array slice solution.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s