Reputation: 25
I would like to add a filter function in my wordpress search query :
Limit the query posts in category 7,8 AND include tags in the search which are in category 7,8
I have already the code to limit to category 7,8 but I don't know how to include tags in the query search and limit these tags to category 7,8
function SearchFilter($query)
{ if ($query->is_search)
{$query->set('cat','7,8');
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
thank you for help :)
Upvotes: 2
Views: 3910
Reputation: 8451
The pre_get_posts
filter (codex page) gives access to the query variable object ( $query
), which is a WP_Query
object. That means you can use all its parameters to filter the query results.
I'll give you some examples of tags parameters, making use of tag slugs or tag ids (single variable or arrays). However, I suggest to take a look at codex examples.
$query->set('tag', 'bread,baking'); // Display posts that have "either" of these tags
$query->set('tag__in', array( 37, 47 )); //Display posts that are tagged with either tag id 37 or 47
$query->set('tag__and', array( 37, 47 )); //Display posts that are tagged with both tag id 37 and tag id 47:
For more details, you can also review the WP_Query codex page.
Upvotes: 3