Skip to content

Instantly share code, notes, and snippets.

@ville6000
Last active October 22, 2015 08:19
Show Gist options
  • Save ville6000/b7045cbe954dcc7091a9 to your computer and use it in GitHub Desktop.
Save ville6000/b7045cbe954dcc7091a9 to your computer and use it in GitHub Desktop.
Exclude pages from WordPress search
<?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