Skip to content

Instantly share code, notes, and snippets.

@thierrypigot
Last active July 27, 2019 11:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thierrypigot/727460d160ef43a027d9 to your computer and use it in GitHub Desktop.
Save thierrypigot/727460d160ef43a027d9 to your computer and use it in GitHub Desktop.
Exclude page / category from search on WordPress frontend
<?php
/**
* Exlcude categories ids: 2, 3 and 5
**/
function tp_SearchFilter($query)
{
if( !$query->is_admin && $query->is_search )
{
$query->set( 'category__not_in', array( 2, 3, 5 ) );
}
return $query;
}
add_filter('pre_get_posts','tp_SearchFilter');
<?php
/**
* Exlcude page ids: 12, 23 and 65
**/
function tp_SearchFilter($query)
{
if( !$query->is_admin && $query->is_search )
{
$query->set( 'post__not_in', array( 12, 23, 65 ) );
}
return $query;
}
add_filter('pre_get_posts','tp_SearchFilter');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment