WordPress.org

Make WordPress Core

Opened 3 years ago

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

Download all attachments as: .zip

Change History (17)

comment:1 t31os_3 years ago

  • Cc wp-t31os@… added

greuben3 years ago

comment:2 greuben3 years ago

  • Keywords has-patch added; needs-patch removed

Thats my try!

greuben3 years ago

comment:3 greuben3 years ago

  • Keywords needs-testing added

comment:4 scribu3 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 3 years ago by scribu (previous) (diff)

comment:5 greuben3 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.

greuben3 years ago

comment:6 follow-up: scribu3 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 3 years ago by scribu (next)

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

comment:8 scribu3 years ago

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

comment:9 scribu3 years ago

Related: #17165

comment:10 scribu3 years ago

Related: #15292

comment:11 nabha3 years ago

  • Cc nabha added

comment:12 follow-up: scribu3 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 greuben3 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.

comment:14 ryan3 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.