How can I combine the result of the tax_query and meta_query?
ex:
tax_query results to:
Post1, Post2.
and meta_query results to:
Post3.
I want to combine the results of the 2 queries.
Here is my partial code:
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'tax1',
'field' => 'term_id',
'terms' => array(1,2,3),
),
array(
'taxonomy' => 'tax2',
'field' => 'term_id',
'terms' => array(1,2,3),
),
),
'meta_query' => array(
array(
'key' => 'meta1',
'value' => '1',
'compare' => '=',
),
),
The same question was asked here but wasn’t answered. So I’m re-opening it.
Thanks!
It sounds like you are wanting it to act like an ‘OR’ where a post that matches either the
tax_query
ormeta_query
is returned.Unfortunately, that’s not possible using WP_Query.
You’ll either need to write a custom database call, or just query them separately and do your checking on the PHP end (that’s ok if you know you’ll have a relatively small number of results, but be warned this won’t necessarily scale to a huge site).
The only way to achieve a query like that is creating a custom query to the db using the
$wpdb
global and SQL statements. I think it would be something like this:Then you could iterate over
$pageposts
as with aWP_Query
loop.