Did you know that WordPress allows you to feature your posts by using sticky posts feature. However, sticky posts are one of the least known features of WordPress. In this article, we will show you 6 cool things you can do with sticky posts in WordPress.
Video Tutorial
If you don’t like the video or need more instructions, then continue reading.
1. Automatically Expire Sticky Posts
If you are using sticky posts to highlight a special event or coupon, then you will need to unstick the post once that event is over.
This sounds like unnecessary work that you should automate.
Simply install and activate the Expire Sticky Posts plugin. Upon activation, you can set expiry date for sticky posts.
After the expiry date, your sticky post will automatically become a normal post. For detailed instructions take a look at our tutorial (with video) on how to set expiration date for sticky posts in WordPress.
2. Sticky Posts for Categories
By default, sticky posts only appear on the front-page of your site. But what if you wanted to display featured content on your category archive pages?
You can do that by installing and activating the Category Sticky Post plugin. Upon activation, edit a post that you want to feature and select the sticky post category.
For more detailed instructions, see our tutorial on how to add sticky posts for categories in WordPress.
3. Display Latest Sticky Posts
Typically sticky posts are used for featured posts to display your most prominent content. But after a while your old featured posts disappear under the archives. You can bring back your old featured content to life by showing them on custom archives page or anywhere else on your site.
Simply paste this code in your theme’s functions.php file or a site-specific WordPress plugin.
function wpb_latest_sticky() { /* 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 */ $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');
After adding this code, simply create add the shortcode [latest_stickies]
wherever you want to display your latest sticky posts.
For detailed instructions, visit our article: How to display latest sticky posts in WordPress.
4. Sticky Posts for Custom Post Types
Sticky post feature is only available for WordPress posts, but this does not mean that you cannot add this feature for other post types.
Simply install and activate the Sticky Custom Post Types plugin. Once you have activated the plugin, visit Settings » Reading and enable sticky posts for any post type you want.
For more detailed instructions check out our tutorial on how to add sticky posts in WordPress custom post types.
5. How to Hide Sticky Posts From WordPress Loop
When using sticky posts, you will notice that by default WordPress displays your sticky post at the top of all your WordPress posts. For example, if you have a loop to show recent posts, then sticky posts will appear on the top no matter when they were added.
To avoid this simply use ignore_sticky_posts
argument in your WordPress query, like this:
<?php $args = array( 'posts_per_page' => 10, 'ignore_sticky_posts' => 1 ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
See our tutorial on how to exclude sticky posts from WordPress loop for more detailed instructions.
6. Styling Sticky Posts
Want to add custom styling to your sticky posts?
Many WordPress themes use post_class()
function to automatically add post classes for each post. If your theme is already using post_class() function, then you will see sticky class added to your sticky posts.
If your theme is not adding sticky class to the post container div, then you can add that yourself by adding post_class() function into the post div or article container.
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
Now you can use the .sticky
CSS class in your child theme‘s stylesheet. Here is some basic CSS to get you started:
.sticky { background-color:#ededed; border:1 px solid #f5f5f5; color:#272727; padding:5px; } .sticky:before { content: "Featured"; color: #FFF; background: #f20000; padding: 10px; display: inline-block; text-align: right; float: right; font-weight: bold; text-transform: uppercase; }
This is how it looked on our demo site using Twenty Twelve theme.
That’s all, we hope this article helped you learn some cool things to do with sticky posts on your WordPress site. You may also want to check out our guide on 10 most wanted category hacks and plugins for WordPress.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.
Tabish says
Is there any easy way to show sticky post or any specific post on homepage without any other post. I mean only sticky post
WPBeginner Support says
You have a few different options, you may want to take a look at our article below:
https://www.wpbeginner.com/wp-tutorials/how-to-display-recent-posts-in-wordpress/
Admin
Rachelle says
The sticky post plugin hasn’t been updated in over two years.
emiliano says
hi,
it’ possible to saves the notes in profile page of users ?
When the users saves a note is automatically saves on profile page
Yaamin says
Everything is involving plugins :/
WPBeginner Staff says
Yes you got the point. First you will install the plugin to enable sticky posts for categories. After that you can make posts sticky for specific categories. Later you can make it unsticky. Your post will remain live and will not give a 404 error.
Dave Gasser says
This site is an excellent source for all things WordPress. I just finished the article on sticky notes and instantly thought of product launches. Post your sticky note squeeze page and program it to auto expire once you are ready to get back to business as usual. Great article and great site.
Rich says
I was just tango-ing with sticky post customization this week. This is a very good overview of sticky post capabilities. Thanks!
Charlie Sasser says
So would some of this be helpful if I wanted to display in a dedicated area only the category “News” that were sticky?Then when the news was stale it would drop off but not be gone and prevent a 401 error? Or is there a better way or plugin to do this for non-coders?