I just had a realisation that in my backend I can search everything in every search field. IE. I have a custom post type called Recipe, which is searchable in the normal Post search area (purely in the back-end). Is there any way to make sure you can only search for the given taxonomy within the post type, custom or not? And also being able to do this, but still search on the front-end, and get results for everything at once?
My function for the search is as follows:
function ScanWPostFilter($query) {
if ($query->is_search) {
$query->set('post_type', array('post','product','recipe', 'page'));
$query->set('orderby', array('relevance' => 'DESC', 'type' => 'ASC'));
}
return $query;
}
add_filter('pre_get_posts','ScanWPostFilter');
2
Answers
To restrict the search to a specific taxonomy within the post type in the backend, you can modify the
ScanWPostFilter
function as follows:Replace
'your_taxonomy'
with the actual taxonomy slug you want to search within.You could do something like this