How to Make WordPress Show the Latest Sticky Posts

WordPress has a built-in featured called “sticky posts.” This puts select content above all others on your website. But what if you want to show the the latest sticky posts only? That’s when you have to make a few modifications to WordPress.

Unfortunately, this means you’ll have to add some code to the core files of WordPress. If you don’t feel comfortable making these edits, you can always spend time looking for a plugin to show the latest sticky posts for you.

However, I was unable to find one that fit my purposes.

In this tutorial, I’ll show you how to make WordPress show the latest sticky posts while using a shortcode. It’s not a difficult process, and I’ll even give you the code to paste into your website.

How to Use Sticky Posts

When you create a new post, you have the option to set it to be “sticky.” This tells WordPress that you prioritize the content and want it to appear first in lists.

You can set this option in the Visibility setting of a post. This option is not available for pages.

Sticky Post

One thing you have to keep in mind, though, is that sticky posts are not supported by every theme. However, it’s not very common for a theme to disable this feature.

You can also use plugins to manage your sticky posts which usually have more features than what WordPress offers.

Inputting the Code

Before you make changes to your site, I suggest setting up a child theme. This will protect your site by saving your edits and modifications if your theme is updated. Otherwise, you could lose all of your adjustments and have to enter them in again.

There are some pros and cons to using a child theme, but usually the benefits outweigh the drawbacks.

Today, we’re going to edit the functions.php file of your theme. You can access these files by using FTP programs like FileZilla or File Manager in cPanel.

To keep things quick and easy, I’m just going to use File Manager.

Go to the root directory of your site and open the “themes” folder.

Open Themes

Open your theme’s folder.

Open Theme

Select the functions.php file and click, “Edit.”

Edit Functions

Paste this code into the file:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]function wpb_latest_sticky() {

/* Get all sticky posts */
$sticky = get_option( ‘sticky_posts’ );

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

/* Get the 5 newest stickies (You can change this to any number by altering the number 5 below) */
$sticky = array_slice( $sticky, 0, 5 );

/* Query sticky posts */
$the_query = new WP_Query( array( ‘post__in’ => $sticky, ‘ignore_sticky_posts’ => 1 ) );
// The Loop
if ( $the_query->have_posts() ) {
$return .= ‘

    ’;
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    $return .= ‘
  • ’ . get_the_title() . ‘
    ’ . get_the_excerpt(). ‘
  • ’;

    }
    $return .= ‘

’;

} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

return $return;

}
add_shortcode(‘latest_stickies’, ‘wpb_latest_sticky’);
[/ht_message]

Paste Code

This creates a new shortcode for us to use called, “[latest_stickies].” It uses the wp-query command to show sticky posts first.

As you can see, I placed the code under the “}” below the if statement for the theme version.

Save your changes to the file.

Placing the Shortcode

To show your latest sticky posts, go to any post or page on your site. You can create a new one if you’d like.

Open Post

Paste the shortcode we just created form the code.

Paste Shortcode

Update or save your post or page.

Update Page

Now, your sticky posts will show in a list according to the latest published date.

Latest Sticky Posts

Can you make the shortcode anything you like?

Yes. In the example above, we called it “latest_stickies.” However you can change this to anything else as long as you edit it in the code. However, I suggest leaving it as-is because it’s an obvious name and easy to remember its purpose. But, it’s up to you.

Will this shortcode work in a text widget for the sidebar?

Yes. Bear in mind that your theme may not allow sticky posts. However, it’s not very common for a theme to do this.

What are some good ways to show the latest sticky posts in WordPress?

In my example above, I used a specific landing page for sticky posts. I can then add this page to the navigation bar and label it something like, “Must See Posts” or something similar.

You can also paste this shortcode under a recent posts that isn’t sticky. This will give visitors something to click at the end of the content if you want them to see important articles.

You’re only limited by your imagination and design ideas, and shortcodes are usable throughout most areas of WordPress including most custom post types created by other plugins.

Engage Your Visitors

Showing the latest sticky posts in WordPress is another way to engage your visitor. It gives them a way to see content you deem to be important, which they may not know is available on your site. It’s all about making your content easier to find, and this method gets your articles in front of more eyes.

What kind of methods do you use to engage your audience? How often do you make a post “sticky?”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.