Resources     Blog  

  1. Home
  2. WordPress Tutorials
  3. How to Make WordPress Show the Latest Sticky Posts

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:

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 .= ‘<ul>’;
while ( $the_query->have_posts() ) {
$the_query->the_post();
$return .= ‘<li><a href=»‘ .get_permalink(). ‘» title=»‘ . get_the_title() . ‘»>’ . get_the_title() . ‘</a><br />’ . get_the_excerpt(). ‘</li>’;

}
$return .= ‘</ul>’;

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

return $return;

}
add_shortcode(‘latest_stickies’, ‘wpb_latest_sticky’);

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?»

Author: Michael Brockbank

Michael has been in the tech industry in some form or another since the late 1990s. He’s built a variety of websites using HTML, Joomla and WordPress. Starting his freelance writing career in 2012, he’s completed thousands of projects world-wide covering a wide scope of topics. Michael is currently the Content Marketing Team Lead. Today, he manages a team of writers to create tutorials, blog posts and support content for customers. Outside of GreenGeeks, he manages a YouTube channel that helps others learn how to become freelance writers, bloggers and overall professionals.

Was this article helpful?

Related Articles

Leave a Comment

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.