Opened 14 years ago
Closed 13 years ago
#15292 closed defect (bug) (fixed)
It's not possible to check for "meta_value = 0" in wp_query->get_posts
Reported by: | hetjens | Owned by: | scribu |
---|---|---|---|
Milestone: | 3.2 | Priority: | normal |
Severity: | major | Version: | 3.0.1 |
Component: | Query | Keywords: | has-patch commit |
Focuses: | Cc: |
Description (last modified by )
Because of the "empty" function of php it isn't possible to check for a meta_value = 0 in wp_query->get_posts.
My Case:
'meta_key' => '_menu_item_menu_item_parent', 'meta_value' => '0',
wp-includes/query.php Line 2307 is:
if ( ! empty($q['meta_value']) ) {
But empty() says that String "0" is true
So the query will return all posts that have a meta-field "_menu_item_menu_item_parent" and not only the posts with "_menu_item_menu_item_parent = 0" (which means, that it's an menu root item)
My solution is:
if (( ! empty($q['meta_value']) ) || ($q['meta_value'] == '0')) {
but that's not very nice. Any better idea?
Thanks,
Philip
Attachments (4)
Change History (19)
#3
@
14 years ago
- Keywords dev-feedback added
- Milestone changed from Awaiting Review to Future Release
In 3.1, you can pass meta_value = '_wp_zero_value'.
We could probably use strlen(), though.
#4
@
14 years ago
- Keywords needs-patch 3.2-early added; query get_post meta meta_value dev-feedback removed
- Owner set to dd32
- Status changed from new to accepted
Closed Ticket #16573 as duplicate.
In 3.1, you can pass meta_value = '_wp_zero_value'.
For any developers coming across this, Please do not use it. Yes, It works in 3.1 but it's an internal hack only.. You cannot rely on this being there for 3.2.
using strlen() on Line 423 will probably fix this ticket.
#5
@
14 years ago
using strlen() on Line 423 will probably fix this ticket.
but would have the effect that you can't query against a literal, empty string.
Closed #16735 as duplicate (cannot query meta_Value != )
#6
@
14 years ago
- Keywords has-patch added; needs-patch removed
16573.diff should cover this ticket as well as #16735.
Also see unit-meta-query.php.
#8
@
14 years ago
This answer made me realize that I shouldn't be using array_filter(). Hence, 16573.2.diff. Also updated the unit tests.
#13
follow-up:
↓ 14
@
13 years ago
Let's remove the zero value hacks introduced in [17088] and make sure wp_dropdown_users() works with this new patch.
Try meta_value_num