Remove Sticky Posts from the Main Loop

We use the pre_get_posts hook to modify the main loop and tell it to ignore sticky posts. Note that this removes them from the blog’s main loop entirely, not just from the top. If you want to display them, you will need to write your own loop above the main loop that only queries those sticky posts:

/**
 * Snippet Name: Remove sticky posts from the main query
 */
function sv_ignore_sticky( $query ) {
	if ( ! is_admin() && $query->is_home() && $query->is_main_query() ) {
		$query->set( 'ignore_sticky_posts', true );
		$query->set( 'post__not_in', get_option( 'sticky_posts' ) );
	}
}

add_action( 'pre_get_posts', 'sv_ignore_sticky' );

The condition limits the change to the main query on the blog home page, so sticky posts still show in widgets, custom loops and the admin. ignore_sticky_posts on its own only stops them from being moved to the top. post__not_in is what removes them from the list.

Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!