I can’t seem to figure out how to include tags in a wordpress search. For example, if I search for ‘apple’, I would like to get back posts with ‘apple’ in the title or the content (default wordpress functionality) as well as posts that contain the tag ‘apple’; When I add the 'tag' => $keyword line into WP_query then I get no results for every search.

$keyword = get_search_query();
$args = array(
    'post_type' =>  array('case_studies', 'news', 'events'),
    'post_status' => 'publish',
    's' => $keyword,
    'tag' => $keyword,
);
$query = new WP_Query($args);

If I remove it, I get results as normal (but without posts with the keyword in its tags)

3 Answers
3

This would only work if the keyword exactly matches the tag that you are searching for, and unfortunately, tag data isn’t indexed by WordPress search by default, so that won’t yield anything either. If you want to index the tags and include that in search results you might want to use a plugin. Relevanassi might do the trick, but if you don’t mind spending a bit, then SearchWP would definitely do it, since it indexes all content, including tags and custom fields, and lets you assign weight to each one. You could for instance, give tags the highest weight, or if you only want to search tags, simply give everything else a weight of 0.

Leave a Reply

Your email address will not be published. Required fields are marked *