How to Ignore Sticky Posts in WordPress: A Simple Guide

Home » Snippets » How to Ignore Sticky Posts in WordPress: A Simple Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

In the world of WordPress, ‘sticky’ posts are those posts that you purposefully keep at the top of your blog, so readers see them first. They can be a great tool for highlighting key content, but there are times when they may not serve your purpose, and you might want them to be ignored. That’s where the skill of ignoring sticky posts comes in handy. In this article, we’ll guide you through the process of ignoring sticky posts in WordPress, helping you manipulate your blog feed to best suit your needs.

					function wpturbo_query_ignore_sticky_posts( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'ignore_sticky_posts', 1 );
    }
}
add_action( 'pre_get_posts', 'wpturbo_query_ignore_sticky_posts' );
				

This snippet helps you modify the default WordPress home page query to ignore sticky posts. This becomes useful when you want all posts on the home page to be displayed chronologically rather than having sticky posts appear at the top regardless of their publishing date.

The function wpturbo_query_ignore_sticky_posts( $query ) is customized to get the job done. This function uses a conditional statement that is examining the main query and determining if it’s the home page being queried.

Inside the function wpturbo_query_ignore_sticky_posts( $query ), there’s an if condition that checks two things:

if ( $query->is_home() && $query->is_main_query() )

The first condition $query->is_home() checks if the home page is being displayed. The second condition $query->is_main_query() checks if the main query is being used. The AND logical operator (&&) in between these two conditions ensures that both conditions must be met for the if statement to evaluate to true.

In case these conditions evaluate to true, we then set the ignore_sticky_posts parameter of the query object to 1 using $query->set( 'ignore_sticky_posts', 1 ); . This signifies that sticky posts should be ignored, thus causing them to appear in their true chronological order among the other posts.

Lastly, the custom function wpturbo_query_ignore_sticky_posts is hooked to pre_get_posts action hook of WordPress. This hook allows you to alter the query before the actual query is run, hence the name, pre_get_posts.

add_action( 'pre_get_posts', 'wpturbo_query_ignore_sticky_posts' );

Overall, this code snippet, when added in the theme’s functions.php file or a site-specific plugin file, will ignore any sticky posts and list all posts on the WordPress Homepage by date.

Register an account to save your snippets or go Pro to get more features.