Last active
October 22, 2015 08:19
-
-
Save ville6000/b7045cbe954dcc7091a9 to your computer and use it in GitHub Desktop.
Exclude pages from WordPress search
This file contains 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 | |
/** | |
* Exclude footer pages from search | |
* | |
* @param $query | |
* | |
* @return array | |
*/ | |
function your_theme_exclude_pages_from_search( $query ) { | |
if ( $query->is_search ) { | |
$footer_page = get_page_by_title( 'Footer' ); | |
$page_ids = array( $footer_page->ID ); | |
if ( ! is_null( $footer_page ) ) { | |
$page = new WP_Query( array( | |
'post_parent' => $footer_page->ID, | |
'post_type' => 'page', | |
) ); | |
if ( $page->have_posts() ) { | |
while ( $page->have_posts() ) { | |
$page->the_post(); | |
$page_ids[] = get_the_ID(); | |
} | |
} | |
$query->set( 'post__not_in', $page_ids ); | |
} | |
} | |
return $query; | |
} | |
add_filter( 'pre_get_posts', 'your_theme_exclude_pages_from_search' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment