How to make WordPress Posts to Stick on top of Other Posts? – Sticky Posts!

Updated on September 2, 2017

I was working on a new project – a site based on WordPress, where I had to display four boxes with each box displaying a post from a category called Services. Well, doing that is quite easy, as I just need to use WP_Query function and run a loop to pull out the posts. By default, the posts are pulled in reverse chronological order with latest posts at the top. It means, the four boxes were displaying the latest post on each. But my client had a different thought – he asked me to display four boxes with the posts that he selects. For example, he wants to decide what posts should be displayed in each of those four boxes and he should be able to change that anytime. Well, he has a point – reason, the boxes are supposed display four key services of his business.

I have been seeing “Stick Post” option whenever I write/edit a post in WordPress, but I never thought that will help me to solve the above issue.

Download : Free Ebook on WordPress.

What’s Sticky Post in WordPress?

Sticky Posts allows you to stick any post on top of other posts. For example, something you see in forums, where a post such as “Forum Rules” will be always in top of other posts. So in my case, the client can make his post of interest as “Sticky Post” and that will have higher priority over other posts (including the latest posts).

How to make Sticky Post in WordPress?

While you write a post, lookout for “Edit” link next to “Visibility: Public” and click it to see “Stick this post to the front page” checkbox – check that box and click “OK” button. When you are done, publish the post to make it Sticky. If you wish, the old posts can also be made sticky.

how to sticky posts

How to display Sticky posts, only?

If you want to display only stick posts, then following is the code.

$args = array(
'post_in' => get_option( 'sticky_posts' )
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>

That’s it. Now my client was able to make posts of his interest as “Sticky” and that was displayed in those four boxes.

How to avoid Sticky posts and display all other posts?

If you want to display recent posts widget, then probably you do not want sticky posts. In that case, you can use “‘ignore_sticky_posts’ => 1

$args = array(
'posts_per_page' => 5,
'ignore_sticky_posts' => 1;
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>

Was this article helpful?

Related Articles

Leave a Comment