[wp-trac] [WordPress Trac] #27193: tax_query returns only partial results
WordPress Trac
noreply at wordpress.org
Mon Feb 24 01:46:57 UTC 2014
#27193: tax_query returns only partial results
--------------------------+-----------------------------
Reporter: p_enrique | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
Querying by taxonomy using WP_Query returns unexpected results. It appears
only the first criteria is matched.
=== Premises ===
Post 1 is in category 'category1'. `$category1` is the term_id.
Post 2 is in category 'category2'. `$category2` is the term_id.
Post 3 is in tagged with 'tag1'. `$tag1` is the term_id.
Post 4 is in tagged with 'tag2'. `$tag2` is the term_id.
{{{
$category1 = $this->factory->term->create( array( 'taxonomy' =>
'category', 'name' => 'alpha' ) );
$category2 = $this->factory->term->create( array( 'taxonomy' =>
'category', 'name' => 'beta' ) );
$tag1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag',
'name' => 'gamma' ) );
$tag2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag',
'name' => 'delta' ) );
$post_id1 = $this->factory->post->create( array( 'post_title' => 'alpha',
'post_category' => array( $category1 ) ) );
$post_id2 = $this->factory->post->create( array( 'post_title' => 'beta',
'post_category' => array( $category2 ) ) );
$post_id3 = $this->factory->post->create( array( 'post_title' => 'gamma',
'post_tag' => array( $tag1 ) ) );
$post_id4 = $this->factory->post->create( array( 'post_title' => 'delta',
'post_tag' => array( $tag2 ) ) );
}}}
=== Testing tax_query for categories 1 and 2 ===
{{{
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( $category1, $category2 )
)
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
}}}
'''Expected result:''' `$posts` contains post 1 and 2.
'''Actual result:''' `$posts` contains post 1.
'''Workaround:''' Add `'relation' => 'OR'` to the tax_query.
=== Testing two taxonomies at once ===
{{{
$query = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( $category1, $category2 )
),
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => array( $tag1, $tag2 )
),
'relation' => 'OR'
)
) );
}}}
'''Expected result:''' `$posts` contains posts 1, 2, 3 and 4.
'''Actual result:''' `$posts` contains posts 1 and 2.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/27193>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list