[wp-trac] [WordPress Trac] #48360: `meta_value` is ignored when empty string is provided

WordPress Trac noreply at wordpress.org
Thu Oct 17 16:46:14 UTC 2019


#48360: `meta_value` is ignored when empty string is provided
--------------------------+------------------------------
 Reporter:  JPry          |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Query         |     Version:  3.2
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------
Description changed by davidbaumwald:

Old description:

> When trying to create a custom query using `meta_*` parameters,
> `WP_Meta_Query` will ignore `meta_value` if it is an empty string.
>
> This does appear to be somewhat intentional, since `WP_Query` will set
> the default value for `meta_value` to an empty string. This can be seen
> here: https://core.trac.wordpress.org/browser/trunk/src/wp-includes
> /class-wp-meta-query.php#L255
>
> The problem comes when you want to write a query that specifically
> targets meta keys that have empty values. The `'meta_value' => ''`
> parameter is stripped, and then the query does not behave properly.
> Here's an example query:
>
> {{{#!php
> <?php
> $args = array(
>     'post_type'      => 'product',
>     'posts_per_page' => 10,
>     'meta_key'       => '_regular_price',
>     'meta_value'     => '',
>     'meta_compare'   => '=',
> );
>
> $query = new WP_Query( $args );
> }}}
>

> I would like to propose that an empty string be allowed for `meta_value`.
> Alternatively, the fact that an empty value will be stripped should at
> least be documented more clearly so that developers know to expect that
> behavior.
>
> Props also to richardbuff for helping identify the issue.

New description:

 When trying to create a custom query using `meta_*` parameters,
 `WP_Meta_Query` will ignore `meta_value` if it is an empty string.

 This does appear to be somewhat intentional, since `WP_Query` will set the
 default value for `meta_value` to an empty string. This can be seen here:
 https://core.trac.wordpress.org/browser/trunk/src/wp-includes/class-wp-
 meta-query.php#L255

 The problem comes when you want to write a query that specifically targets
 meta keys that have empty values. The `'meta_value' => ''` parameter is
 stripped, and then the query does not behave properly. Here's an example
 query:

 {{{#!php
 <?php
 $args = array(
     'post_type'      => 'product',
     'posts_per_page' => 10,
     'meta_key'       => '_regular_price',
     'meta_value'     => '',
     'meta_compare'   => '=',
 );

 $query = new WP_Query( $args );
 }}}


 I would like to propose that an empty string be allowed for `meta_value`.
 Alternatively, the fact that an empty value will be stripped should at
 least be documented more clearly so that developers know to expect that
 behavior.

 Props also to @richardbuff for helping identify the issue.

--

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


More information about the wp-trac mailing list