[wp-trac] [WordPress Trac] #56370: Taxonomy parameter in a WP_Query not merged correctly with already existing taxonomy query with OR relation.
WordPress Trac
noreply at wordpress.org
Fri Aug 12 15:57:56 UTC 2022
#56370: Taxonomy parameter in a WP_Query not merged correctly with already existing
taxonomy query with OR relation.
--------------------------+-----------------------------
Reporter: hugod | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
When creating a WP_Query with a taxonomy slug in parameter and a taxonomy
query with the OR relation, all queried terms are merge under the OR
relation.
Here is an example, given this query:
{{{#!php
<?php
$q = new WP_Query(
array(
'fields' => 'ids',
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'foo' => 'foo-term',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => array( $cat_term_1 ),
'field' => 'term_id',
),
array(
'taxonomy' => 'category',
'terms' => array( $cat_term_2 ),
'field' => 'term_id',
),
),
)
);
}}}
WP_Query::parse_tax_query() transforms WP_Query->tax_query->queries into:
{{{#!php
<?php
$resulting_query = array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => array( $cat_term_1 ),
'field' => 'term_id',
'operator' => 'IN',
'include_children' => true,
),
array(
'taxonomy' => 'category',
'terms' => array( $cat_term_2 ),
'field' => 'term_id',
'operator' => 'IN',
'include_children' => true,
),
array(
'taxonomy' => 'foo',
'terms' => array(
'foo-term',
),
'field' => 'slug',
'operator' => 'IN',
'include_children' => true,
),
);
}}}
Although I expect it to be:
{{{#!php
<?php
$expected = array(
'relation' => 'AND',
array(
'taxonomy' => 'foo',
'terms' => 'foo-term',
'field' => 'slug',
'operator' => 'IN',
'include_children' => true,
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => array( $cat_term_1 ),
'field' => 'term_id',
'operator' => 'IN',
'include_children' => true,
),
array(
'taxonomy' => 'category',
'terms' => array( $cat_term_2 ),
'field' => 'term_id',
'operator' => 'IN',
'include_children' => true,
),
array(
'taxonomy' => 'foo',
'terms' => array(
'foo-term',
),
'field' => 'slug',
'operator' => 'IN',
'include_children' => true,
),
),
);
}}}
Indeed, the relation AND is the default one. See
[https://github.com/WordPress/wordpress-develop/blob/6.0/src/wp-includes
/class-wp-tax-query.php#L97-L98].
WordPress merges all queried terms here https://github.com/WordPress
/wordpress-develop/blob/6.0.1/src/wp-includes/class-wp-
query.php#L1163-L1168 without looking at the relations.
I am aware that querying with the taxonomy slug as parameter in WP_Query
is deprecated, but the same issue occurs with category for instance with a
query like this:
{{{#!php
<?php
$q = new WP_Query(
array(
'post_type' => 'post',
'cat' => 5,
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'custom_tax',
'field' => 'slug',
'terms' => array( 'test-tax' )
),
array(
'taxonomy' => 'custom_tax',
'field' => 'slug',
'terms' => array( 'test-tax-2' )
)
)
)
);
}}}
But for category, WordPress merges terms here
[https://github.com/WordPress/wordpress-develop/blob/6.0.1/src/wp-includes
/class-wp-query.php#L1196-L1203].
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56370>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list