[wp-trac] [WordPress Trac] #33341: WP_Meta_Query IN operator with empty array does not return expected result
WordPress Trac
noreply at wordpress.org
Tue Aug 11 18:32:27 UTC 2015
#33341: WP_Meta_Query IN operator with empty array does not return expected result
--------------------------+-----------------------------
Reporter: flixos90 | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
When using the `IN` compare mode in a `WP_Meta_Query` where the value is
an empty array, this query is evaluated with an unexpected behavior.
For example, I have some posts with a meta field called `'some-
textfield'`:
{{{
$posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'some-textfield',
'value' => array(),
'compare' => 'IN',
),
),
) );
}}}
This code returns all posts although I would rather expect it to return no
posts at all since the value of `'some-textfield'` (whatever it may be) is
not ''in'' those provided in the `value` field.
I discovered it when I needed to perform a meta query where the value was
an array that was returned by a previous operation. It is obviously a
minor issue since we can simply check if the array is empty (and in that
case do not make the query at all) - but I thought it's not really the
expected result of such a query.
The solution would be to:
* ignore this condition if the `relation` is set to `OR` and there are
other conditions available
* evaluate this condition to false otherwise (and thus return no results)
--
Ticket URL: <https://core.trac.wordpress.org/ticket/33341>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list