All Resources

Hide Specific Pages from WordPress Search Results

A situation that comes up often is wanting to hide certain pages from WordPress search results. This can be achieved pretty easily using the pre_get_posts hook in your functions.php file. Just pass in an array of page IDs to the 'post__not_in' WP_Query parameter (note the double underscores) and you’re good to go.

function your_custom_search_filter($query) {
if (!is_admin() && $query->is_search) {
$exclude_ids = array(1,2,3); //page ids you want to exclude
$query->set('post__not_in', $exclude_ids);
return $query;
} ?>

view raw


hosted with ❤ by GitHub

April 26, 2015 WordPress Development