wp query – WordPress search WP_Query to cover multiple post types and their custom fields?
I am trying to make the WP default search a bit more intelligent, by having it use custom fields. It searches through my custom post types, but only for the usual fields (excerpt, title, content).
In my arguments to WP_Query I specify all the post types. But not sure how to specify precisely all the custom fields with an OR
because I am essentially looking for content in every post type.
The code below doesn’t work, gives 0 results for the keyword quality
for example. But a direct search in MySQL on the wp_postmeta
table suggests there are 12 posts with the word quality in it. I want my query to bring out all of them.
What’s the right way to include them in the WP_Query?
- No filters / actions
- No plugins I’d like to do this directly in WP_Query for now, and it needs to support two languages. I’ll only ever need to query one language in each search engine (PolyLang).
The code:
$SEARCH = $_GET['s'];
$args = array(
's' => $SEARCH
,'orderby' => 'relevance'
,'post_type' => array('page', 'services', 'resources', 'events', 'reports', 'posts')
,'post_status' => 'publish'
,'posts_per_page' => 15
,'meta_query' => array(
'relation' => 'OR'
// SEARCH THROUGH "reports" post type's custom fields
,array(
'key' => 'report_content',
'value' => "$SEARCH",
'compare' => 'LIKE'
)
,array(
'key' => 'report_details',
'value' => "$SEARCH",
'compare' => 'LIKE'
)
// SEARCH THROUGH "events" post type's custom fields
,array(
'key' => 'event_brief',
'value' => "$SEARCH",
'compare' => 'LIKE'
)
)
);
What I am trying to do above is to look for the word in the variable $SEARCH inside those custom fields.
Leave an answer