Free Wordpress Blog Setup

How to Display the Latest Sticky Posts in WordPress

By Editorial Staff in Tutorials
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.

<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );

/* Query sticky posts */
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>

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.

Free Wordpress Blog Setup

Comments

2 Responses to “How to Display the Latest Sticky Posts in WordPress”
  1. Chris says:

    This is a website for beginners, can’t you dumb this down a little bit more?

    • We offer tutorials for all levels of WordPress Users. Beginners guide category and Showcase category are mostly for beginners. Theme editing, Tutorials, require prior knowledge. To make template changes, you need to know what the loop is. We provided the link in the article that explains what the loop is. Unfortunately, we cannot define what loop is in all of our articles because it is a concept that is a must know before you try to do any template editing.

      We explained what each code in the query is doing. If you need individual help, you may use our contact form to contact us.

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar