Make WordPress Core

Opened 5 years ago

Last modified 5 years ago

#15292 closed defect (bug)

It's not possible to check for "meta_value = 0" in wp_query->get_posts — at Version 7

Reported by: hetjens Owned by: dd32
Milestone: 3.2 Priority: normal
Severity: major Version: 3.0.1
Component: Query Keywords: has-patch commit
Focuses: 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?


Change History (9)

#1 @nacin
5 years ago

Try meta_value_num

#2 @hetjens
5 years ago

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

#3 @mdawaffe
5 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 @dd32
5 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 @dd32
5 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 != )

5 years ago

#6 @scribu
5 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.

5 years ago

Don't use array_filter()

#7 @scribu
5 years ago

  • Description modified (diff)
Note: See TracTickets for help on using tickets.