Make WordPress Core

Opened 14 years ago

Closed 13 years ago

#16829 closed enhancement (fixed)

Automatically set 'compare' => 'IN', when meta value is an array

Reported by: scribu's profile scribu Owned by: duck_'s profile duck_
Milestone: 3.3 Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch
Focuses: Cc:

Description (last modified by scribu)

This should just work:

get_posts( array(
  'meta_key' => 'foo',
  'meta_value' => array( 'bar', baz' )
) );

Currently, you have to remember to set meta compare:

get_posts( array(
  'meta_key' => 'foo',
  'meta_value' => array( 'bar', baz' ),
  'meta_compare' => 'IN'
) );

Obviously, it should be the same when using 'meta_query'.

Attachments (3)

meta_compare.diff (664 bytes) - added by ldebrouwer 14 years ago.
Patch that defaults $meta_compare to 'IN' if $qcompare? is not set and $qvalue? is an array.
meta_compare_v2.diff (700 bytes) - added by ldebrouwer 14 years ago.
Same patch, just relative to the WP root dir unlike the previous one.
16829.patch (1.4 KB) - added by SergeyBiryukov 13 years ago.

Download all attachments as: .zip

Change History (19)

#1 @scribu
14 years ago

  • Description modified (diff)

#2 @scribu
14 years ago

  • Description modified (diff)

@ldebrouwer
14 years ago

Patch that defaults $meta_compare to 'IN' if $qcompare? is not set and $qvalue? is an array.

#3 @ldebrouwer
14 years ago

  • Cc ldebrouwer added
  • Keywords has-patch added; needs-patch removed

Added the patch, sorry for the crappy description of it. It that defaults $meta_compare to 'IN' if $meta_compare is not set and $meta_value is an array.

Version 0, edited 14 years ago by ldebrouwer (next)

#4 @ldebrouwer
14 years ago

  • Keywords needs-testing added

Needs testing of course..

#5 @scribu
14 years ago

Please make your patches relative to the wp root dir, not to wp-includes.

#6 @ldebrouwer
14 years ago

Sorry about that!

@ldebrouwer
14 years ago

Same patch, just relative to the WP root dir unlike the previous one.

#7 @scribu
13 years ago

  • Milestone changed from Future Release to 3.3

Marked #18401 as dup.

#8 follow-up: @scribu
13 years ago

Note that if you're trying to match against an actual array as a value, you will have to manually apply maybe_serialize() on it. Also, you can still explicitly set the 'compare' parameter to '='.

#9 in reply to: ↑ 8 @Doggie52
13 years ago

  • Cc douglas@… added

Replying to scribu:

Note that if you're trying to match against an actual array as a value, you will have to manually apply maybe_serialize() on it. Also, you can still explicitly set the 'compare' parameter to '='.

What do you mean? If what you're matching against (i.e. the DB entry) is serialized? If you know that data is serialized then you'll of course serialize your value - am I wrong? Why point such a thing out?

#10 @scribu
13 years ago

Because some people might expect that when using meta_query, the value will automatically be serialized for them, like it is when using update_post_meta(), for instance.

#11 @ldebrouwer
13 years ago

Maybe that's something for the codex page on get_posts().

#12 @scribu
13 years ago

No, I don't think we should mention in on the Codex, since storing arrays in custom fields and then trying to match against them is a bad idea anyway.

#13 @duck_
13 years ago

  • Keywords needs-refresh added; needs-testing removed

Looks good, but needs a refresh. Would need to move the code defining and validating $meta_compare to below $meta_value = $q['value']; (line 740).

Also, does anybody else think it would be a bit more readable if is_array( $meta_value ) sets the default which gets overridden if isset( $q['compare'] )? For example:

$meta_compare = is_array( $meta_value ) ? 'IN' : '=';
if ( isset( $q['compare'] ) )
    $meta_compare = strtoupper( $q['compare'] )

#14 @scribu
13 years ago

Yes, that would be more readable.

#15 @SergeyBiryukov
13 years ago

  • Keywords needs-refresh removed

#16 @duck_
13 years ago

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

In [18825]:

Automatically set 'compare' => 'IN' in WP_Meta_Query::get_sql() when the meta value is an array. Props ldebrouwer, SergeyBiryukov. Fixes #16829.

Note: See TracTickets for help on using tickets.