WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 8 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 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 8 years ago.
16573.2.diff (1.9 KB) - added by scribu 8 years ago.
Don't use array_filter()
unit-meta-query.php (1.9 KB) - added by scribu 8 years ago.
16573.3.diff (2.7 KB) - added by scribu 8 years ago.

Download all attachments as: .zip

Change History (19)

#1 @nacin
9 years ago

Try meta_value_num

#2 @hetjens
9 years ago

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

#3 @mdawaffe
9 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
9 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
8 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 != )

@scribu
8 years ago

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

@scribu
8 years ago

Don't use array_filter()

#7 @scribu
8 years ago

  • Description modified (diff)

#8 @scribu
8 years ago

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

#9 @scribu
8 years ago

  • Keywords 3.2-early removed
  • Milestone changed from Future Release to 3.2

#10 @scribu
8 years ago

  • Owner changed from dd32 to scribu

#11 @scribu
8 years ago

  • Keywords commit added

#12 @scribu
8 years ago

Related: #17011

#13 follow-up: @ryan
8 years ago

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

@scribu
8 years ago

#14 in reply to: ↑ 13 @scribu
8 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

#15 @ryan
8 years ago

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