search ]

Remove Sticky Posts from the Main Loop

We use a filter called pre_get_posts to modify the main loop and tell it to ignore sticky posts. Note that this will cause them not to appear anywhere when you call the loop. 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_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' );
Join the 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!

Savvy WordPress Development official logo