Last active
August 29, 2015 14:24
-
-
Save chuckreynolds/77e1c44a42b99d61bbc1 to your computer and use it in GitHub Desktop.
filter stuff from wordpress search results
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
/** | |
* Exclude a page and children pages from wp search by making an array | |
* of page id's starting wtih 2927 == id (can't use page slugs) | |
*/ | |
add_filter( 'pre_get_posts', 'filter_the_search' ); | |
function filter_the_search( $query ) { | |
if ( $query->is_search ) { | |
$pages = array( 2927 ); // parent page ID | |
foreach( $pages as $page ) { | |
$childrens = get_pages( array( 'child_of' => $page, 'echo' => 0 ) ); | |
} | |
for( $i = 0; $i < sizeof( $childrens ); ++$i ) { | |
$pages[] = $childrens[$i]->ID; | |
} | |
$query->set( 'post__not_in', $pages ); | |
} | |
return $query; | |
} | |
/** | |
* example limit to .. | |
*/ | |
add_filter( 'pre_get_posts','filter_the_search' ); | |
function filter_the_search( $query ) { | |
if ( $query->is_search ) { | |
$query->set( 'pagename', 'this-page,that-page,contact' ); | |
} | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for sub pages we should be able to use
post_parent
(int)