WP_Query not using relation key as expected and not producing any results
-
I have created a custom post type
service-provider
with custom taxonomiesregion
andservice-category
– By itself, this search works as expected BUT I want to also search on an ACF field.I have an ACF field on the
service-provider
post type calledorganisation
I have added a
meta_query
withrelation
=OR
to search with the keyword and find a match either in the post content OR the ACF field.I have a post with Title = “Accommodation Service” and ACF Field “organisation” = “Dawn Inc”
I want the search to show results where the keyword matches either the post title or the acf field.
Here is my query:
<?php // Handle form submission $keyword = isset( $_GET['keyword'] ) ? sanitize_text_field( $_GET['keyword'] ) : ''; $region = isset( $_GET['region'] ) ? array_map('sanitize_text_field', $_GET['region']) : array(); $service_category = isset( $_GET['service-category'] ) ? array_map('sanitize_text_field', $_GET['service-category']) : array(); if ( !empty($keyword) || !empty($service_category) ) : $args = array( 'post_type' => 'service-provider', 'posts_per_page' => -1, 's' => $keyword, 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'organisation', 'value' => $keyword, 'compare' => 'LIKE', ) ), ); if ( ! empty( $region ) ) { $args['tax_query'][] = array( 'taxonomy' => 'region', 'field' => 'slug', 'terms' => $region, ); } if ( ! empty( $service_category ) ) { $args['tax_query'][] = array( 'taxonomy' => 'service-category', 'field' => 'slug', 'terms' => $service_category, ); } $query = new WP_Query( $args ); ?>
When I search for “dawn” I get no results. So I dumped the
$query
to see the SQL and this is what I got:SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND (((wp_posts.post_title LIKE '%dawn%') OR (wp_posts.post_excerpt LIKE '%dawn%') OR (wp_posts.post_content LIKE '%dawn%'))) AND ( ( wp_postmeta.meta_key = 'organisation' AND wp_postmeta.meta_value LIKE '%dawn%' ) ) AND ((wp_posts.post_type = 'service-provider' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private'))) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title LIKE '%dawn%' DESC, wp_posts.post_date DESC
I see that the
meta_query
is performing an AND relation.When I run this query straight in MySQL I get no results BUT, if I change the
AND
to anOR
on this line:FROM:
AND ( ( wp_postmeta.meta_key = 'organisation' AND wp_postmeta.meta_value LIKE '%dawn%' ) )
TO:
OR ( ( wp_postmeta.meta_key = 'organisation' AND wp_postmeta.meta_value LIKE '%dawn%' ) )
I get the result I am expecting to see…
So, if I have already set the meta_query to
OR
and it is giving me this result – how to I fix this query to give me the results I expect?
- The topic ‘WP_Query not using relation key as expected and not producing any results’ is closed to new replies.