Opened 3 years ago

Closed 2 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
Priority: normal Milestone: 3.2
Component: Query Version: 3.0.1
Severity: major Keywords: has-patch commit
Cc:

Description (last modified by scribu)

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)

16573.diff (1.9 KB) - added by scribu 2 years ago.
16573.2.diff (1.9 KB) - added by scribu 2 years ago.
Don't use array_filter()
unit-meta-query.php (1.9 KB) - added by scribu 2 years ago.
16573.3.diff (2.7 KB) - added by scribu 2 years ago.

Download all attachments as: .zip

Change History (19)

Try meta_value_num

meta_value_num is only for orderby and not for checking against it.

  • 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.

comment:4   dd322 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.

comment:5   dd322 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 != )

scribu2 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.

scribu2 years ago

Don't use array_filter()

  • Description modified (diff)

This answer made me realize that I shouldn't be using array_filter(). Hence, 16573.2.diff. Also updated the unit tests.

scribu2 years ago

  • Keywords 3.2-early removed
  • Milestone changed from Future Release to 3.2
  • Owner changed from dd32 to scribu
  • Keywords commit added

Related: #17011

comment:13 follow-up: ↓ 14   ryan2 years ago

Let's remove the zero value hacks introduced in [17088] and make sure wp_dropdown_users() works with this new patch.

scribu2 years ago

comment:14 in reply to: ↑ 13   scribu2 years ago

Replying to ryan:

Let's remove the zero value hacks introduced in [17088] and make sure wp_dropdown_users() works with this new patch.

Done: 16573.3.diff

  • Resolution set to fixed
  • Status changed from accepted to closed

(In [17674]) Allow querying empty meta values. Props scribu. fixes #15292

Note: See TracTickets for help on using tickets.