Opened 2 years ago

Closed 2 years ago

#17011 closed enhancement (fixed)

Introduce 'relation' arg to meta_query

Reported by: scribu Owned by:
Priority: normal Milestone: 3.2
Component: Query Version:
Severity: normal Keywords: has-patch needs-testing
Cc: wp-t31os@…, nabha

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 2 years ago.
17011.2.diff (2.2 KB) - added by greuben 2 years ago.
17011.3.diff (2.1 KB) - added by greuben 2 years ago.

Download all attachments as: .zip

Change History (17)

  • Cc wp-t31os@… added

greuben2 years ago

  • Keywords has-patch added; needs-patch removed

Thats my try!

greuben2 years ago

  • Keywords needs-testing added

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 2 years ago by scribu (previous) (diff)

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.

greuben2 years ago

comment:6 follow-up: ↓ 7   scribu2 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?

Version 0, edited 2 years ago by scribu (next)

comment:7 in reply to: ↑ 6   greuben2 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.

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

Related: #17165

Related: #15292

  • Cc nabha added

comment:12 follow-up: ↓ 13   scribu2 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.

comment:13 in reply to: ↑ 12   greuben2 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.

  • 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.