...

How to Exclude Sticky Posts from the Loop in WordPress

196

Sticky Posts are a great way to highlight your featured content. However, there are certain places on your website where you don’t need sticky posts to be on the top. WordPress can not guess this so you need to explicitly tell WordPress to exclude sticky posts from a custom loop. In this article, we will show you how to completely exclude sticky posts from the loop in WordPress, and we will also show you how you can take away the sticky feature of the post, so it still shows in their natural order.

How to take away the Sticky Ability of the Post

When you are displaying most recent posts in a tab, you do not want the sticky posts to stay sticky. If you do not remove the sticky feature, the recent posts area would be useless as all your sticky posts will crowd this area. This is where query_posts feature comes in handy.

To do this you will need to change your loop to something 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();  ?>

This code ignores that a post is sticky and shows the posts in the normal order. Using this code your sticky posts will appear in the loop, however they will not be placed on the top.

Sticky posts displayed in normal order

Completely exclude Sticky posts from the Loop

If you are using sticky posts in a slider, then sometimes you might want to completely exclude your sticky posts from the loop. All what you have to do is edit your custom loop to match with this:

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

This code will not display any sticky posts in the post loop. For more tips on modifying WordPress themes, check out our WordPress Theme Cheat Sheet for beginners.

Source: WP Codex

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.