How to add "OR" relation in "tax_query" and "meta_query". Because the default is "AND"
[query] => Array
(
[relation] => OR
[posts_per_page] => 3
[offset] => 0
[orderby] => post_date
[order] => DESC
[post_type] => biography
[post_status] => publish
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[key] => f_name
[value] => gh
[compare] => LIKE
)
[1] => Array
(
[key] => l_name
[value] => gh
[compare] => LIKE
)
)
[tax_query] => Array
(
[relation] => OR
[0] => Array
(
[taxonomy] => historical-period
[field] => term_id
[terms] => Array
(
[0] => 27
)
[operator] => IN
)
)
)
We got the below result
Result
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (27))
**AND **
(( wp_postmeta.meta_key = 'f_name' AND wp_postmeta.meta_value LIKE '%gh%' ) OR ( wp_postmeta.meta_key = '%gh%' )) AND wp_posts.post_type = 'biography' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 3
We need the below result
Result
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (27))
**OR**
(( wp_postmeta.meta_key = 'f_name' AND wp_postmeta.meta_value LIKE '%gh%' ) OR ( wp_postmeta.meta_key = '%gh%' )) AND wp_posts.post_type = 'biography' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 3
Please help to adding a "OR" relation between "tax_query" and "meta_query"
I have also tried the below code. But we have not gotten an accurate result.
- filter_query
add_filter('get_meta_sql', 'filter_query', 10, 1);
$posts = new WP_Query($post_args);
remove_filter('get_meta_sql', 'filter_query');
function filter_query($sql){
$pos = strpos($sql['where'], 'AND');
if ($pos !== false) {
$sql['where'] = substr_replace($sql['where'], 'OR', $pos, strlen('AND'));
}
return $sql;
}
Reference link:
How to give OR relation in tax_query and meta_query WordPress
Problem:
if we used the above code it added "OR" relations in the whole query including the meta_key and meta_value relation.
- Manually merge the queried posts
$photos_query = new WP_Query( $photos );
$quotes_query = new WP_Query( $quotes );
$result = new WP_Query();
// start putting the contents in the new object
$result->posts = array_merge( $photos_query->posts, $quotes_query->posts );
// here you might wanna apply some sort of sorting on $result->posts
// we also need to set post count correctly so as to enable the looping
$result->post_count = count( $result->posts );
Problem: If we used the above code we have found so many duplicate results. and it taking so much time. Sometime it not returning the result because query execution session time out.
Reference link:
2
Answers
I have tried to solve multiple ways to add a "OR" relation between "tax_query" and "meta_query" But still, we not getting accurate results. So at the last, I used WordPress SQL query and got a result.
Thanks Nigel Ren, CBroe and, 4efirrr your support.
SQL: