Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#10053 closed defect (bug) (fixed)

get_children() returns false when no children are found

Reported by: Denis-de-Bernardy Owned by: ryan
Milestone: 2.9 Priority: normal
Severity: minor Version: 2.7
Component: Query Keywords: has-patch commit
Focuses: Cc:


It should return an empty array instead.

Attachments (2)

10053.diff (413 bytes) - added by Denis-de-Bernardy 9 years ago.
10053.2.diff (879 bytes) - added by Denis-de-Bernardy 9 years ago.

Download all attachments as: .zip

Change History (13)

#1 @Denis-de-Bernardy
9 years ago

Found while doing the following query in the loop:

$images = get_children(array(
	'post_parent' => $post->post_parent,
	'post_type' => 'attachment',
	'post_mime_type' => 'image',
	'order_by' => 'menu_order ASC, ID ASC',

#2 @Denis-de-Bernardy
9 years ago

on a non-image attachment page, even.

#3 @Denis-de-Bernardy
9 years ago

  • Summary changed from get_children() returns null when no posts are found to get_children() returns false when no children are found

#4 @Denis-de-Bernardy
9 years ago

  • Keywords has-patch added

so, basically, the patch makes it return a variable initialized to array() instead of false. checked the WP code, and it's not expecting a value of false anywhere.

this makes the behavior of get_children() consistent with that of get_posts()

#5 @scribu
9 years ago

  • Milestone changed from 2.8 to 2.9

This can wait until 2.9, no?

#6 @scribu
9 years ago

  • Version changed from 2.8 to 2.7

#7 @scribu
9 years ago

By the way, doesn't get_posts() also return false when no posts are found? We should be consistent about this.

You usually have to do something different when you don't find any items, and it's easier to check if ( $children ) than if ( empty($children) ).

#8 @Denis-de-Bernardy
9 years ago

It can definitely wait, yes. :-)

(bool) array() returns false and is faster than empty(array()).

#9 @scribu
9 years ago

Yes, but it's still not faster than $boolean == false. :P

#10 @Denis-de-Bernardy
9 years ago

  • Keywords commit added

uploading a slightly improved patch in a sec. it is harmless for WP. on the contrary, it:

  • actually enhances get_media_items()
  • prevents a potential warning in adjacent_image_link()
  • prevents a potential warning in get_children() itself (false is constant, rather than a reference to a variable)

#11 @markjaquith
9 years ago

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

(In [11756]) Return an empty array instead of false for get_children() when no children found. props Denis-de-Bernardy. fixes #10053

Note: See TracTickets for help on using tickets.