Opened 2 years ago
Closed 20 months ago
#16829 closed enhancement (fixed)
Automatically set 'compare' => 'IN', when meta value is an array
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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)
Change History (19)
ldebrouwer — 2 years ago
comment:3
ldebrouwer — 2 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.
Please make your patches relative to the wp root dir, not to wp-includes.
comment:6
ldebrouwer — 2 years ago
Sorry about that!
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 '='.
- 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
scribu — 22 months 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
ldebrouwer — 22 months ago
Maybe that's something for the codex page on get_posts().
comment:12
scribu — 21 months 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_ — 20 months 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
scribu — 20 months ago
Yes, that would be more readable.
SergeyBiryukov — 20 months ago
- Keywords needs-refresh removed
comment:16
duck_ — 20 months ago
- Owner set to duck_
- Resolution set to fixed
- Status changed from new to closed
In [18825]:

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