Skip to content

Snippet: Exclude posts already shown in previous “grids” from displaying in grids to follow

If you have a page with multiple grids and you want to make sure that posts already shown on the first grid are being excluded from following grids this will require custom code because grid modules are not "connected" in anyway you need to actually hook into WordPress to create an array of already displayed posts which you can then use to exclude from following queries.

add_action( 'init', function() {

	if ( ! is_page( 'the-page-slug-to-run-code-on' ) ) {
		return; // this makes sure the code is only being added as needed as to not break archives

	global $displayed_posts;

	$my_displayed_posts = array();

	add_action( 'the_title', function($title) {
		global $my_displayed_posts;
		$my_displayed_posts[] = get_the_ID();
		return $title; // don't mess with the title
	} );

	add_filter( 'pre_get_posts', function($query) {
		global $my_displayed_posts;
		$query->set( 'post__not_in', $my_displayed_posts );
	} );

} );
All PHP snippets should be added via child theme's functions.php file or via a plugin (we recommend WPCode)
Back To Top