Opened 2 years ago

Closed 20 months ago

#16829 closed enhancement (fixed)

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

Reported by: scribu Owned by: duck_
Priority: normal Milestone: 3.3
Component: Query Version:
Severity: normal Keywords: has-patch
Cc: ldebrouwer, douglas@…

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 2 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 2 years ago.
Same patch, just relative to the WP root dir unlike the previous one.
16829.patch (1.4 KB) - added by SergeyBiryukov 20 months ago.

Download all attachments as: .zip

Change History (19)

  • Description modified (diff)
  • Description modified (diff)

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

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

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

Last edited 2 years ago by ldebrouwer (previous) (diff)
  • Keywords needs-testing added

Needs testing of course..

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

Sorry about that!

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

  • Milestone changed from Future Release to 3.3

Marked #18401 as dup.

comment:8 follow-up: ↓ 9   scribu22 months 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 '='.

comment:9 in reply to: ↑ 8   Doggie5222 months 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?

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.

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

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.

  • 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'] )

Yes, that would be more readable.

  • Keywords needs-refresh removed
  • 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.