OR for a single taxonomy in a tax_query

I have a tax_query that has a value added like this:

$query->query_vars['tax_query'][] = array(
    'taxonomy' => 'location',
    'include_children' => true,
    'field' => 'county',
    'terms' => array($_REQUEST['s-location']),
    'operator' => 'IN'
);

And I would now like to add this as a OR to the above. The overall relation for the tax_query still needs to be AND, but I’d just like this taxonomy to be with an OR in between. How could I achieve this?

$query->query_vars['tax_query'][] = array(
    'taxonomy' => 'location',
    'include_children' => true,
    'field' => "JSON_EXTRACT(ads_value, '$.paadress')",
    'terms' => '%'.$cname.'%',
    'operator' => 'LIKE'
);

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

So in the comments you said:

We’re in the middle of an address system migration and this query
needs to return objects where one of these matches. Either the old
system (county field) is set to an id, or the address value has a
match to the search word

If you look at the documentation, custom term fields like the county and ads_value (or JSON_EXTRACT()) in your case are not supported, and the LIKE is also not a standard value for operator; however, since you said that “Separately, yes they work.“, then let’s assume you’ve got some custom code which handle those custom fields and operator? 🤔

And I think all that you need to do is place those two conditions/clauses into a single clause with an OR relation like so:

$tax_query = (array) ( $query->query_vars['tax_query'] ?? array() );

// Add a (named) top-level clause with an OR relation.
$tax_query['location'] = array(
    'relation' => 'OR',
);

// Sub-clause 1: Search in the 'county' field.
// *You should check if $_REQUEST['s-location'] is set.
$tax_query['location'][] = array(
    'taxonomy'         => 'location',
    'include_children' => true,
    'field'            => 'county',
    'terms'            => array( $_REQUEST['s-location'] ),
    'operator'         => 'IN',
);

// Sub-clause 2: Search in the ads_value field.
$tax_query['location'][] = array(
    'taxonomy'         => 'location',
    'include_children' => true,
    'field'            => "JSON_EXTRACT(ads_value, '$.paadress')",
    'terms'            => '%' . $cname . '%',
    'operator'         => 'LIKE',
);

$query->query_vars['tax_query'] = $tax_query;

/* Or short version:
$query->query_vars['tax_query'][] = array(
    'relation' => 'OR',

    // sub-clause 1
    array(
        'taxonomy' => 'location',
        'field'    => 'county',
        'terms'    => array( $_REQUEST['s-location'] ),
        'operator' => 'IN',

    ),

    // sub-clause 2
    array(
        'taxonomy' => 'location',
        'field'    => "JSON_EXTRACT(ads_value, '$.paadress')",
        'terms'    => '%' . $cname . '%',
        'operator' => 'LIKE',
    ),
);
*/

Or did I get it wrong? If so, let me know and I’ll adjust my answer accordingly.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x