Skip to content

Instantly share code, notes, and snippets.

@robincornett
Created October 31, 2017 15:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robincornett/d7ca5d1d898642ba4e93d45b88d9f3ce to your computer and use it in GitHub Desktop.
Save robincornett/d7ca5d1d898642ba4e93d45b88d9f3ce to your computer and use it in GitHub Desktop.
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