Make WordPress Core

Opened 13 years ago

Closed 13 years ago

#17011 closed enhancement (fixed)

Introduce 'relation' arg to meta_query

Reported by: scribu's profile 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)

17011.diff (2.1 KB) - added by greuben 13 years ago.
17011.2.diff (2.2 KB) - added by greuben 13 years ago.
17011.3.diff (2.1 KB) - added by greuben 13 years ago.

Download all attachments as: .zip

Change History (17)

#1 @t31os_
13 years ago

  • Cc wp-t31os@… added

@greuben
13 years ago

#2 @greuben
13 years ago

  • Keywords has-patch added; needs-patch removed

Thats my try!

@greuben
13 years ago

#3 @greuben
13 years ago

  • Keywords needs-testing added

#4 @scribu
13 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';
	}
Version 0, edited 13 years ago by scribu (next)

#5 @greuben
13 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.

@greuben
13 years ago

#6 follow-up: @scribu
13 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? i.e. with the default AND relation.

Last edited 13 years ago by scribu (previous) (diff)

#7 in reply to: ↑ 6 @greuben
13 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.

#8 @scribu
13 years ago

Ah, right, because the same post can have both fields.

#9 @scribu
13 years ago

Related: #17165

#10 @scribu
13 years ago

Related: #15292

#11 @nabha
13 years ago

  • Cc nabha added

#12 follow-up: @scribu
13 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 @greuben
13 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.

#14 @ryan
13 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [17699]) Introduce WP_Meta_Query and relation support. Props scribu, greuben. fixes #17165 #17011

Note: See TracTickets for help on using tickets.