Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to remove landing pages from site search results. Assumes that templates are in the templates directory inside the theme.
<?php
add_action( 'pre_get_posts', 'leaven_search_hide_landing_page' );
/**
* Remove landing pages from the search results.
*
* @param $query \WP_Query
*
* @return mixed
*/
function leaven_search_hide_landing_page( $query ) {
if ( $query->is_search && $query->is_main_query() && ! is_admin() ) {
$query->set( 'meta_query', array(
'relation' => 'OR',
array(
'key' => '_wp_page_template',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_wp_page_template',
'value' => 'templates/page_landing.php',
'compare' => '!=',
),
) );
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.