WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 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)

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

Download all attachments as: .zip

Change History (17)

#1 @t31os_
6 years ago

  • Cc wp-t31os@… added

@greuben
6 years ago

#2 @greuben
6 years ago

  • Keywords has-patch added; needs-patch removed

Thats my try!

@greuben
6 years ago

#3 @greuben
6 years ago

  • Keywords needs-testing added

#4 @scribu
6 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';
	}
Last edited 6 years ago by scribu (previous) (diff)

#5 @greuben
6 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
6 years ago

#6 follow-up: @scribu
6 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 6 years ago by scribu (previous) (diff)

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

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

#9 @scribu
6 years ago

Related: #17165

#10 @scribu
6 years ago

Related: #15292

#11 @nabha
6 years ago

  • Cc nabha added

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