Opened 14 years ago
Closed 14 years ago
#17011 closed enhancement (fixed)
Introduce 'relation' arg to meta_query
Reported by: | scribu | Owned by: | |
---|---|---|---|
Milestone: | 3.2 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Query | Keywords: | has-patch needs-testing |
Focuses: | Cc: |
Description
Just like tax_query has an optional 'relation' arg that allows 'OR' relationships between queries, so should meta_query.
Attachments (3)
Change History (17)
#4
@
14 years ago
What's the deal with $distinct = 'DISTINCT';
?
Also, it's cleaner to set $relation this way:
if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) { $relation = 'OR'; } else { $relation = 'AND'; }
#5
@
14 years ago
If DISTINCT
isn't used the result includes same posts again and again(because of inner joins)
Steps to reporduce:
- Create a post with two meta keys 'foo' and 'bar'
- Create a post with meta key 'foo'
- Create a post with meta key 'bar'
- Run this query
$q = new WP_Query( array( 'fields' => 'ids', 'ignore_sticky_posts' => true, 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'foo', 'value' => array( 'foobar' ), 'compare' => 'IN' ), array( 'key' => 'bar', 'value' => array( 'foobar2' ), 'compare' => 'IN' ) ) ) ); print_r( $q->posts );
Also, it's cleaner to set $relation this way:
Agreed.
#6
follow-up:
↓ 7
@
14 years ago
If DISTINCT isn't used the result includes same posts again and again(because of inner joins)
Ok, but doesn't this happen without the patch as well?
#7
in reply to:
↑ 6
@
14 years ago
Replying to scribu:
If DISTINCT isn't used the result includes same posts again and again(because of inner joins)
Ok, but doesn't this happen without the patch as well? i.e. with the default AND relation.
IIRC only OR'ed queries with inner joins cause that problem.
#12
follow-up:
↓ 13
@
14 years ago
Ah, but the DISTINCT is still unnecessary, since there's a GROUP BY ID clause automatically added on any taxonomy or meta query.
#13
in reply to:
↑ 12
@
14 years ago
Replying to scribu:
Ah, but the DISTINCT is still unnecessary, since there's a GROUP BY ID clause automatically added on any taxonomy or meta query.
Yes, but currently GROUP BY is set only if meta_key
is set, if queried using meta_query
there is no GROUP BY and hence DISTINCT is needed. However, DISTINCT is unnecessary once #17165 goes in and the patch will need refresh.
Thats my try!