Creating a search function for meta data and content
-
Hi there.
I am not that experienced with PHP, queries and all that. I normally do mostly front-end work, but now I am going more towards PHP as well.
I have a page with around 15 pages and 160 posts. I need a way to search for Title and The content of posts and pages, AND I need to be able to search for a meta value in my posts. I have over 20 meta_keys in posts and I need to be able to search for, example: %apartment%. So that the text should contain the word Apartment.
I know how to do a WP search to search for title and content of pages and posts, and I know how to search for meta value (Maybe not in a good way), but I do not know how I combine these.
This is what I have right now:
$search_query = get_search_query(); $args = array( 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => '***', 'value' => $search_query, 'compare' => 'LIKE' ) ) ); $my_query = new WP_Query( $args ); global $wp_query;I remove my meta_query in the $args, and put: ‘s’ => $search_query, then I will be able to search for title and content in wordpress, but most of my page consists of data in the meta_value fields of posts. So I kinda need to be able to search for that as well.
Anyone that is able to help me with this? I have been searching on Google for 2 days now without finding out what I need and what is working. This solution that I have now is the closest that I have gotten.
Hope someone could help since I can’t move on without it
-
You’re very close with what you have. Omit the ‘key’ array element in the meta query. Then WP will find the search value in postmeta.meta_value column regardless of key value. Do not include the SQL wildcard char
%in your search term, WP will assume you want them and add them itself.If you must only find a meta value in several keys but not any key, you can supply an array of key values that are to be searched as the ‘key’ arg.
Also add a ‘s’ array element to the main array with your search term so the query will also look for the term in title, excerpt, or content. WP assumes you want
ANDlogic applied between the ‘s’ and ‘meta_query’ args, that there needs to be a match in both args. ButORlogic is applied within the various ‘s’ fields searched. The ‘s’ search term and ‘meta_query’ search terms don’t have to be the same, but could be.WP_Query does not have an option to specify
ORlogic between ‘s’ and ‘meta_query’. If you need such logic, use the “posts_request” filter to directly alter the SQL. Use PHP string search and replace methods to find the right “AND” and replace it with “OR”.Thank you.
The problem I see right now is that when I do add the ‘s’ at the top there that receives $search_query, then nothing happends.
Only when I removed the ‘s’, I did get a search result, but then only for meta data.You talking about relation. To I have to add ‘relation’ => ‘OR’ after ‘meta_query’ => array, before the second array ?
Instead of re-inventing the wheel, this plugin searches title, content, and meta. You can select what you want it to search:
https://en-ca.ww.wp.xz.cn/plugins/wp-extended-search/
You can also look at the source code if you want to copy the implementation.
There’s also these instructions https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/ if you don’t want to use a plugin.
Lukasz’ plugin suggestion sounds promising, but to answer your question, when there’s only one inner array for “meta_query”, there’s no reason to provide any meta query relation arg since there’s only one meta query arg — there’s nothing to OR against.
Sounds like you want OR logic between the ‘s’ criteria and meta query criteria? That’s not possible with WP_Query args alone, you need to adjust the SQL through the “posts_request” filter. Or use the plugin Lukasz suggested.
A 3rd option is to compose your own SQL query and run it with methods of the global
$wpdbconnection object.
The topic ‘Creating a search function for meta data and content’ is closed to new replies.