[wp-trac] [WordPress Trac] #33341: WP_Meta_Query IN operator with empty array does not return expected result

WordPress Trac noreply at wordpress.org
Thu Aug 20 01:37:36 UTC 2015


#33341: WP_Meta_Query IN operator with empty array does not return expected result
--------------------------+----------------------
 Reporter:  flixos90      |       Owner:
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:
Component:  Query         |     Version:  3.2
 Severity:  normal        |  Resolution:  wontfix
 Keywords:  dev-feedback  |     Focuses:
--------------------------+----------------------
Changes (by boonebgorges):

 * status:  new => closed
 * resolution:   => wontfix
 * milestone:  Awaiting Review =>


Comment:

 I agree with flixos90 that an empty array should be interpreted so that no
 records are matched.

 The problem is that the WordPress never handles `IN` params like this. See
 `post__in`, `post_parent__in`, various `include` params (like
 `get_terms()`), and so forth. All of these are processed like this:

 {{{
 if ( ! empty( $include ) ) {
     // ...
     $sql[] = "ID NOT IN {$include_sql}";
 }
 }}}

 Your suggestion would be something like:

 {{{
 if ( is_array( $include ) || ! empty( $include ) ) { // ...
 }}}

 But if we change the expected behavior in one place, we have to change it
 everywhere. And we can't do this, as it'll surely break lots of plugins.

 As a workaround, I suggest that your code converts clauses like the one
 you suggest to something like this when it's determined that the value
 array is empty:

 {{{
 array(
     'key' => 'some-textfield',
     'compare' => 'NOT EXISTS',
 )
 }}}

 More generally, use `array( 0 )` for `post__in`, `include`, etc. Not
 ideal, but the best we can do given what we've got.

 (I know there have been other tickets where this issue has been discussed,
 but I can't find them right now.)

 Thanks for the ticket, flixos90 :)

--
Ticket URL: <https://core.trac.wordpress.org/ticket/33341#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list