WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#16829 closed enhancement (fixed)

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

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

Download all attachments as: .zip

Change History (19)

comment:1 scribu3 years ago

  • Description modified (diff)

comment:2 scribu3 years ago

  • Description modified (diff)

ldebrouwer3 years ago

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

comment:3 ldebrouwer3 years ago

  • 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 3 years ago by ldebrouwer (previous) (diff)

comment:4 ldebrouwer3 years ago

  • Keywords needs-testing added

Needs testing of course..

comment:5 scribu3 years ago

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

comment:6 ldebrouwer3 years ago

Sorry about that!

ldebrouwer3 years ago

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

comment:7 scribu3 years ago

  • Milestone changed from Future Release to 3.3

Marked #18401 as dup.

comment:8 follow-up: scribu3 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 '='.

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

comment:10 scribu3 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.

comment:11 ldebrouwer3 years ago

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

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

comment:13 duck_3 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'] )

comment:14 scribu3 years ago

Yes, that would be more readable.

SergeyBiryukov3 years ago

comment:15 SergeyBiryukov3 years ago

  • Keywords needs-refresh removed

comment:16 duck_3 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.