How can I tell the default WordPress search function to also look at the postmeta data?
For example, I have a postameta key called ‘item_number’ and if someone searches ‘113’, I’d like for it to check the key’s value as well as the post content.
Thanks!
A quick and dirty way to include meta values inside your searching scope: you can hook into
pre_get_posts
and add ameta_query
like this:Inside your functions.php:
The only problem with this route is that it will include the meta query so that your search query will only match posts that include the search term inside the Title/Content/Excerpt and the meta field, which might not be preferable for you.
You must replace your WordPress search function with your own. Check this answer.
Very simple workaround: put all meta values you want to be searchable into
post_exerpt
field (use save_post action), so they will be found without any search modifications.This site provided the answer I was looking for:
http://flav36rs.com/2010/03/15/extend-wordpress-search-to-include-custom-post-meta/
There are some helpful comments below the tutorial worth looking at if you run into problems.
EDIT:
The above link is dead.
Ultimately, I ended up writing a complete custom search query using JOINS to build the query I needed.