Trying to build a search that not only searches the defaults (title, content etc) but also a specific custom field.
My current query:
$args = array(
'post_type' => 'post',
's' => $query,
'meta_query' => array(
array(
'key' => 'speel',
'value' => $query,
'compare' => 'LIKE'
)
)
);
$search = new WP_Query( $args )
...
This returns posts which match both the search query AND the meta query, but I would also like it to also return posts where it simply matches either one of them.
Any ideas?
I have been searching for hours for a solution to this problem. Array merging is not the way to go, especially when the queries are complex and you must be able to add to meta queries in the future. The solution which is simplistically beautiful is to change ‘s’ to one which allows both searching titles and meta fields.
Usage:
A lot of code can be reduced by using a modified version of this answer.
I have optimized @Stabir Kira answer a bit
Now you can search by (title, content, excrept) or (meta field) or (both of them).
i had the same problem, for my new site i just added a new meta “title” :
functions.php
And then.. just add something like that :
What do you think about this workaround ?
As per Nick Perkins’ suggestion, I had to merge two queries like so:
Well its kind of a hack but it works. You need to add posts_clauses filter. This filter function check for the any of the query word exists in the custom field “speel” and the remaining query stays intact.
I couldn’t find a solution to look for multiple keywords that can be mixed in either post title, description AND/OR one or several metas, so I made my own addition to the search function.
All you need is to add the following code in function.php, and whenever you use the ‘s’ argument in a standard WP_Query() function and want it to search in one or several meta fields as well, you simply add a
's_meta_keys'
argument that is an array of the meta(s) key(s) you want to search in:Example use:
This example will look for the keywords “kewords to search” in post titles, descriptions, and meta keys ‘short_desc’ and ‘tags’.
Keywords can be found in one or several of the fileds, in any order, it will return any post that has all the keywords in any of the designated fields.
You can obiously force the search in a list of meta keys you include in the fonction and get rid of the extra agruments if you want ALL search queries to include these meta keys 🙂
Hope that will help anyone who face the same issue I did!
All of the above solutions only return results if a match exists in the speel meta key. If you have results elsewhere but not in this field you’ll get nothing. Nobody wants that.
A left join is needed. The following will create one.
Here’s another way, just change the request with the ‘posts_where_request’ filter. Everything will still be the default except (‘s’ AND ‘meta_query’) => (‘s’ OR ‘meta_query’).
this is the code
for me works perfect the next code:
I found an clean solution in WordPress core.
WordPress developers already had this problem for search in attachments
_wp_attached_file
meta and they fix this issue in this function:Taking the idea from this function, I wrote the following code to search in metadata:
This is a great solution but you need to fix one thing.
When you call ‘post__in’ you need to set an array of ids and $unique is an array of posts.
example:
@satbir-kira answer works great but it will only search through the meta and post title. If you want it to search through meta, title and content, here is the modified version.
And here is it’s usage:
Replace
$get['search']
with your search value