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 | Owned by: | duck_ |
---|---|---|---|
Milestone: | 3.3 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Query | Keywords: | has-patch |
Focuses: | Cc: |
Description (last modified by )
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)
#3
@
14 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.
#8
follow-up:
↓ 9
@
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
@
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
@
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.
#12
@
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
@
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'] )
Patch that defaults $meta_compare to 'IN' if $qcompare? is not set and $qvalue? is an array.