Exclude Sticky Posts out of Post Loop in WordPress

Posts in WordPress are organized into a post loop. This is the list of posts that appears on your homepage, or when you visit the blog section. It can be helpful to exclude certain types of posts from appearing in this loop; for example, sticky posts which don’t change very often and would clutter up your site’s home page with unimportant content.

Sticky Posts are a great way to highlight your featured content. However, some areas of the site need sticky posts on the top while others don’t. To exclude sticky posts, you need certain knowledge of WordPress programming.

Sticky Posts can be really helpful if properly utilized so let’s go over how they work and where they should or shouldn’t show up in order to make sure that all users get the right visitor experience no matter their preference.

In this article we will show you how to exclude sticky posts out of post loops in WordPress by making use of query parameters.

Remove sticky posts’ orders

When displaying a list of recent posts in WordPress, you will want to exclude the stickies from that set. If your blog has lots of sticky posts, they will take all top space of blog list and make it hard for visitors to view recent posts.

What you need to do in this situation is to keep the sticky posts in the list and make it order like normal posts.

$args = array(
    'posts_per_page' => 20,
    'ignore_sticky_posts' => 1
);
$new_query = new WP_Query( $args );

if ( $new_query->have_posts() ) {	
	while ( $new_query->have_posts() ) {
		$new_query->the_post();	
	}
}

Exclude sticky posts completely

Sticky posts are a great way to make sure something is always above the fold on your page. However, if you already have sticky posts displayed somewhere on that same page then sometimes it can be beneficial to exclude those from being in the loop and instead just display them only when they’re clicked by visitors.

$args = array(
    'post_type' => 'post', 
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'post__not_in' => get_option('sticky_posts'),
);
$new_query = new WP_Query( $args );

if ( $new_query->have_posts() ) {	
	while ( $new_query->have_posts() ) {
		$new_query->the_post();	
	}
}

Exclude from Homepage

In order to exclude a sticky post from the blog’s home page, you will need to make some changes in your theme file.

Look for your theme’s home page in theme folder. The file can be front-page.php, home.php, or index.php. This template contains HTML code that displays posts on the homepage of your site.

After identifying the correct template file, follow one of the 2 code blocks mentioned above to exclude sticky posts.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top