By default, a search in WordPress will only search a post’s title and content. Through filters, it can be extended to include taxonomies, such as categories and tags. This does not have to just be tags and categories, custom taxonomies can also be included within the search.
Send in the filters
Use posts_join filter to perform an INNER JOIN of the terms, term_relationship, and term_taxonomy tables on the posts table.
To include a clause to search the text being search, the posts_where filter is available to append an extra where clause.
The included taxonomy names can be narrowed down to category and post_tags. Wildcards are appended on both sides of the search criteria against the term name.
Finally, the posts_groupby filter is used to aggregate all the post IDs that will fall into out query to match the where clause.
The results may vary
You may not get what you expect. Categories and tags are usually single words. If a search contains more than one word, most categories and tags will be skipped.
This could be avoided by chunking the search words and creating a clause for each chunk against the query. This method could produce more overhead and probably not recommended for larger database.
It should also be noted that including a wild card search on category and tag names isn’t exactly appropriate in most cases. This would best be used with custom taxonomies such as actors for a movie database.
[link_button label=”View the source code on Github” url=”https://github.com/rfmeier/rfm-taxonomy-search”]