Created
October 31, 2017 15:16
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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