Opened 5 years ago
Last modified 5 years ago
#48360 new defect (bug)
`meta_value` is ignored when empty string is provided
Reported by: | JPry | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 3.2 |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description (last modified by )
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 $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.
I'd love to get this sorted (since I've dealt with this before) but here's what the meta query class states right now:
I can relate with the use case and we can update the default behavior for meta queries, and probably patch it in WP_Query so it passes null as a default value. However, I'm not sure how robust this would be, especially with third-party plugins relying on this core behavior.