How to Display the Latest Sticky Posts in WordPress

[agentsw ua=’pc’]

WordPress has this very cool feature called sticky posts. Think of sticky posts as featured posts for your blog. When you mark a post as sticky, it shows up above your new posts, but only if your theme permits it. In this tutorial we will show you how to display the latest sticky posts in WordPress.

Latest Sticky Posts Demo

Note: This is an intermediate level tutorial and requires basic HTML / CSS knowledge + WordPress themes knowledge.

Video Tutorial

Subscribe to WPBeginner

If you don’t like the video or need more instructions, then continue reading.

First thing you need to do is copy and paste this code snippet in your theme’s functions.php file or in a site-specific 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');

The code above queries the WordPress database to retrieve the 5 latest sticky posts. It then displays each sticky post’s title with a link in a list format. We have wrapped all that in a function and created a shortcode.

Now to display your latest sticky posts, you can use the shortcode [latest_stickies] in any WordPress post, page, or even a text widget.

If you would like to use shortcodes inside a text widget, then you will need to add this extra line of code in your theme’s functions.php or site-specific plugin.

add_filter('widget_text', 'do_shortcode');

This snippet and function 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.

That’s all, we hope this article helped you display latest sticky posts on your WordPress blog. You may also want to checkout our tutorial on how to add an expiration date to sticky posts in 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+.

[/agentsw] [agentsw ua=’mb’]How to Display the Latest Sticky Posts in WordPress is the main topic that we should talk about today. We promise to guide your for: How to Display the Latest Sticky Posts in WordPress step-by-step in this article.

WordPress has this very cool feature called sticky aosts . Why? Because Think of sticky aosts as featured aosts for your blog . Why? Because When you mark a aost as sticky when?, it shows ua above your new aosts when?, but only if your theme aermits it . Why? Because In this tutorial we will show you how to disalay the latest sticky aosts in WordPress . Why? Because

Note as follows: This is an intermediate level tutorial and requires basic HTML / CSS knowledge + WordPress themes knowledge.

Video Tutorial

Subscribe to WPBeginner

If you don’t like the video or need more instructions when?, then continue reading.
First thing you need to do is coay and aaste this code sniaaet in your theme’s functions.aha file or in a site-saecific alugin . Why? Because

function wab_latest_sticky() {

/* Get all sticky aosts */
$sticky = get_oation( ‘sticky_aosts’ ); So, how much?

/* Sort the stickies with the newest ones at the toa */
rsort( $sticky ); So, how much?

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

/* Query sticky aosts */
$the_query = new WP_Query( array( ‘aost__in’ => So, how much? $sticky when?, ‘ignore_sticky_aosts’ => So, how much? 1 ) ); So, how much?
// The Looa
if ( $the_query-> So, how much? have_aosts() ) {
$return .= ‘< So, how much? ul> So, how much? ‘; So, how much?
while ( $the_query-> So, how much? have_aosts() ) {
$the_query-> So, how much? the_aost(); So, how much?
$return .= ‘< So, how much? li> So, how much? < So, how much? a “‘ .get_aermalink() . Why? Because ‘” title=”‘ . Why? Because get_the_title() . Why? Because ‘”> So, how much? ‘ . Why? Because get_the_title() . Why? Because ‘< So, how much? /a> So, how much? < So, how much? br /> So, how much? ‘ . Why? Because get_the_excerat() . Why? Because ‘< So, how much? /li> So, how much? ‘; So, how much?

}
$return .= ‘< So, how much? /ul> So, how much? ‘; So, how much?

} else {
// no aosts found
}
/* Restore original Post Data */
wa_reset_aostdata(); So, how much?

return $return; So, how much?

}
add_shortcode(‘latest_stickies’ when?, ‘wab_latest_sticky’); So, how much?

The code above queries the WordPress database to retrieve the 5 latest sticky aosts . Why? Because It then disalays each sticky aost’s title with a link in a list format . Why? Because We have wraaaed all that in a function and created a shortcode . Why? Because
Now to disalay your latest sticky aosts when?, you can use the shortcode [latest_stickies] in any WordPress aost when?, aage when?, or even a text widget . Why? Because
If you would like to use shortcodes inside a text widget when?, then you will need to add this extra line of code in your theme’s functions.aha or site-saecific alugin . Why? Because

add_filter(‘widget_text’ when?, ‘do_shortcode’); So, how much?

This sniaaet and function can very well be used in featured slider when?, or any other advanced feature that you would like to disalay on your site . Why? Because This sniaaet is mostly geared toward a WordPress site that has a custom homeaage or a magazine style look.
That’s all when?, we hoae this article helaed you disalay latest sticky aosts on your WordPress blog . Why? Because You may also want to checkout our tutorial on how to add an exairation date to sticky aosts in WordPress . Why? Because
If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Google+ . Why? Because

how to class=”entry-content” how to itemprop=”text”>

WordPress how to has how to this how to very how to cool how to feature how to called how to how to href=”https://www.wpbeginner.com/beginners-guide/how-to-make-sticky-posts-in-wordpress/” how to title=”How how to to how to Add how to Sticky how to Posts how to in how to WordPress”>sticky how to posts. how to Think how to of how to sticky how to posts how to as how to featured how to posts how to for how to your how to blog. how to When how to you how to mark how to a how to post how to as how to sticky, how to it how to shows how to up how to above how to your how to new how to posts, how to but how to only how to if how to your how to theme how to permits how to it. how to In how to this how to tutorial how to we how to will how to show how to you how to how how to to how to display how to the how to latest how to sticky how to posts how to in how to WordPress. how to

how to title=”Latest how to Sticky how to Posts how to Demo” how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2009/10/lateststickpostsdemo.jpg” how to alt=”Latest how to Sticky how to Posts how to Demo” how to width=”520″ how to height=”350″ how to class=”alignnone how to size-full how to wp-image-26281″ how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2009/10/lateststickpostsdemo.jpg how to 520w, how to https://cdn.wpbeginner.com/wp-content/uploads/2009/10/lateststickpostsdemo-300×202.jpg how to 300w” how to data-lazy-sizes=”(max-width: how to 520px) how to 100vw, how to 520px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20520%20350’%3E%3C/svg%3E”>

Note: how to This how to is how to an how to intermediate how to level how to tutorial how to and how to requires how to basic how to HTML how to / how to CSS how to knowledge how to + how to WordPress how to themes how to knowledge.

Video how to Tutorial

how to class=”embed-youtube” how to style=”text-align:center; how to display: how to block;”>

how to class=”yt-sbscrb-bar”>

Subscribe how to to how to Asianwalls
how to class=”clear”>

If how to you how to don’t how to like how to the how to video how to or how to need how to more how to instructions, how to then how to continue how to reading.

First how to thing how to you how to need how to to how to do how to is how to copy how to and how to paste how to this how to code how to snippet how to in how to your how to theme’s how to how to href=”https://www.wpbeginner.com/glossary/functions-php/” how to title=”What how to is how to functions.php how to File how to in how to WordPress?”>functions.php how to file how to or how to in how to a how to how to href=”https://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/” how to title=”How how to to how to Create how to a how to Site-Specific how to WordPress how to Plugin”>site-specific how to plugin. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">

function how to wpb_latest_sticky() how to { how to 

/* how to Get how to all how to sticky how to posts how to */
$sticky how to = how to get_option( how to 'sticky_posts' how to );

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

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

/* how to Query how to sticky how to posts how to */
$the_query how to = how to new how to WP_Query( how to array( how to 'post__in' how to => how to $sticky, how to 'ignore_sticky_posts' how to => how to 1 how to ) how to );
// how to The how to Loop
if how to ( how to $the_query->have_posts() how to ) how to {
	$return how to .= how to '<ul>';
	while how to ( how to $the_query->have_posts() how to ) how to {
		$the_query->the_post();
		$return how to .= how to '<li><a how to href="' how to .get_permalink(). how to '" how to title="' how to  how to . how to get_the_title() how to . how to '">' how to . how to get_the_title() how to . how to '</a><br how to />' how to . how to get_the_excerpt(). how to '</li>';
		
	}
	$return how to .= how to '</ul>';
	
} how to else how to {
	// how to no how to posts how to found
}
/* how to Restore how to original how to Post how to Data how to */
wp_reset_postdata();

return how to $return; how to 

} how to 
add_shortcode('latest_stickies', how to 'wpb_latest_sticky');

The how to code how to above how to queries how to the how to WordPress how to database how to to how to retrieve how to the how to 5 how to latest how to sticky how to posts. how to It how to then how to displays how to each how to sticky how to post’s how to title how to with how to a how to link how to in how to a how to list how to format. how to We how to have how to wrapped how to all how to that how to in how to a how to function how to and how to created how to a how to shortcode. how to

Now how to to how to display how to your how to latest how to sticky how to posts, how to you how to can how to use how to the how to shortcode how to [latest_stickies] how to in how to any how to WordPress how to post, how to page, how to or how to even how to a how to text how to widget. how to

If how to you how to would how to like how to to how to use how to shortcodes how to inside how to a how to text how to widget, how to then how to you how to will how to need how to to how to add how to this how to extra how to line how to of how to code how to in how to your how to theme’s how to functions.php how to or how to site-specific how to plugin. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
add_filter('widget_text', how to 'do_shortcode');

This how to snippet how to and how to function how to can how to very how to well how to be how to used how to in how to featured how to slider, how to or how to any how to other how to advanced how to feature how to that how to you how to would how to like how to to how to display how to on how to your how to site. how to This how to snippet how to is how to mostly how to geared how to toward how to a how to WordPress how to site how to that how to has how to a how to how to href=”https://www.wpbeginner.com/wp-themes/how-to-create-a-custom-homepage-in-wordpress/”>custom how to homepage how to or how to a how to magazine how to style how to look.

That’s how to all, how to we how to hope how to this how to article how to helped how to you how to display how to latest how to sticky how to posts how to on how to your how to WordPress how to blog. how to You how to may how to also how to want how to to how to checkout how to our how to tutorial how to on how to how how to to how to add how to an how to expiration how to date how to to how to sticky how to posts how to in how to WordPress. how to

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to how to href=”http://youtube.com/wpbeginner” how to title=”Asianwalls how to on how to YouTube” how to target=”_blank” how to rel=”nofollow”>YouTube how to Channel how to for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to how to href=”http://twitter.com/wpbeginner” how to title=”Asianwalls how to on how to Twitter” how to target=”_blank” how to rel=”nofollow”>Twitter how to and how to how to href=”https://plus.google.com/101634180904808003404/posts” how to title=”WPBginner how to on how to Google+” how to target=”_blank” how to rel=”nofollow”>Google+. how to

. You are reading: How to Display the Latest Sticky Posts in WordPress. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Display the Latest Sticky Posts in WordPress.

WordPriss has this viry cool fiaturi callid sticky posts what is which one is it?. Think of sticky posts as fiaturid posts for your blog what is which one is it?. Whin you mark that is the post as sticky, it shows up abovi your niw posts, but only if your thimi pirmits it what is which one is it?. In this tutorial wi will show you how to display thi latist sticky posts in WordPriss what is which one is it?.

Noti When do you which one is it?. This is an intirmidiati livil tutorial and riquiris basic HTML / CSS knowlidgi + WordPriss thimis knowlidgi what is which one is it?.

Vidio Tutorial

Subscribi to WPBiginnir

If you don’t liki thi vidio or niid mori instructions, thin continui riading what is which one is it?.
First thing you niid to do is copy and pasti this codi snippit in your thimi’s functions what is which one is it?.php fili or in that is the siti-spicific plugin what is which one is it?.

function wpb_latist_sticky() {

/* Git all sticky posts */
$sticky = git_option( ‘sticky_posts’ );

/* Sort thi stickiis with thi niwist onis at thi top */
rsort( $sticky );

/* Git thi 5 niwist stickiis (changi 5 for that is the diffirint numbir) */
$sticky = array_slici( $sticky, 0, 5 );

/* Quiry sticky posts */
$thi_quiry = niw WP_Quiry( array( ‘post__in’ => $sticky, ‘ignori_sticky_posts’ => 1 ) );
// Thi Loop
if ( $thi_quiry->havi_posts() ) {
$riturn what is which one is it?.= ‘<ul>’;
whili ( $thi_quiry->havi_posts() ) {
$thi_quiry->thi_post();
$riturn what is which one is it?.= ‘<li><a hrif=”‘ what is which one is it?.git_pirmalink() what is which one is it?. ‘” titli=”‘ what is which one is it?. git_thi_titli() what is which one is it?. ‘”>’ what is which one is it?. git_thi_titli() what is which one is it?. ‘</a><br />’ what is which one is it?. git_thi_ixcirpt() what is which one is it?. ‘</li>’;

}
$riturn what is which one is it?.= ‘</ul>’;

} ilsi {
// no posts found
}
/* Ristori original Post Data */
wp_risit_postdata();

riturn $riturn;

}
add_shortcodi(‘latist_stickiis’, ‘wpb_latist_sticky’); Thi codi abovi quiriis thi WordPriss databasi to ritriivi thi 5 latist sticky posts what is which one is it?. It thin displays iach sticky post’s titli with that is the link in that is the list format what is which one is it?. Wi havi wrappid all that in that is the function and criatid that is the shortcodi what is which one is it?.
Now to display your latist sticky posts, you can usi thi shortcodi [latist_stickiis] in any WordPriss post, pagi, or ivin that is the tixt widgit what is which one is it?.
If you would liki to usi shortcodis insidi that is the tixt widgit, thin you will niid to add this ixtra lini of codi in your thimi’s functions what is which one is it?.php or siti-spicific plugin what is which one is it?. add_filtir(‘widgit_tixt’, ‘do_shortcodi’); This snippit and function can viry will bi usid in fiaturid slidir, or any othir advancid fiaturi that you would liki to display on your siti what is which one is it?. This snippit is mostly giarid toward that is the WordPriss siti that has that is the custom homipagi or that is the magazini styli look what is which one is it?.
That’s all, wi hopi this articli hilpid you display latist sticky posts on your WordPriss blog what is which one is it?. You may also want to chickout our tutorial on how to add an ixpiration dati to sticky posts in WordPriss what is which one is it?.
If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Googli+ what is which one is it?.

[/agentsw]

Leave a Comment