Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Display the Latest Sticky Posts in WordPress

Last updated on by
Follow WPBeginner on YouTube
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.

    /* 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.

Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • João G.

    Btw, i also can’t thank you guys enough for this wonderful article!! I’m very new in loop related issues (and lets face it, it is a very tricky part in WordPress), going beyond the basic loop is an adventure!
    Until this article I had myself a custom loop, but was acting wierd, apart from the main loop (that i wasn’t even using!) to do what i wanted…. now I can use the main loop like a boss =)
    I saw that the main loop can be filtered in functions.php using the new hook ‘pre_get_posts’, wich i heard is more clean (since wordpress only does 1 query the way we want instead of remaking the query like this case)

    Do you guys have an article about that? If not please consider that =) and contact me, I will read it for sure!

  • João G.

    Jesus Christ! Sometimes things are complicated, because we make them that way….. the basic loop (like the editorial staff said, everyone should know at least this before even messing with this kind of business) is:

    // your miracle coding here

    and like they say, in a very explicit and not confusing way, just paste the code they provided before the loop (The code i provided)…. if you don’t understand what i just said, spend some hours learning in here –>

    • João G.

      rrrr the code is:

      if ( have_posts() ) : while ( have_posts() ) : the_post(); (surrounded by php tags)

      //miracle coding here

      endwhile; endif; (surrounded by php tags)

  • Rick Nielsen

    How would the code look if I wanted to display only a custom post type that I created?

    Thanks! Great work!

  • aljuk

    Can’t thank you enough, just what I was looking for!

    I’ve used it like this, to show the latest sticky (if one exists) or the latest post (if there are no stickies) :

    if (get_option(‘sticky_posts’)) {
    $sticky = get_option(‘sticky_posts’);
    $sticky = array_slice( $sticky, 0, 1 );
    else {

    followed by the loop.

  • Chris

    I have to agree with Pete. This article is too much theory and note enough practice (i.e. usage examples).

  • Pete

    Sorry, I’m none the wiser. :(

  • Pete

    Can you modify this (show us the loop code as well) to show us how to display a simple list of sticky posts?

    thanks :)

    • Editorial Staff

      Use the loop link we recommend to look at the codex. We will not add the line if have post, while have post. There is no need to repeat ourselves, that is why we link to other articles such as making custom pages, and the loop page in the codex. So this simple article doesn’t become overbloated.

  • Chris

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

    • Editorial Staff

      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.