Make WordPress Core

Opened 17 years ago

Closed 17 years ago

#10053 closed defect (bug) (fixed)

get_children() returns false when no children are found

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

Description

It should return an empty array instead.

Attachments (2)

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

Download all attachments as: .zip

Change History (13)

#1 @Denis-de-Bernardy
17 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
17 years ago

on a non-image attachment page, even.

#3 @Denis-de-Bernardy
17 years ago

  • Summary get_children() returns null when no posts are foundget_children() returns false when no children are found

#4 @Denis-de-Bernardy
17 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
17 years ago

  • Milestone 2.82.9

This can wait until 2.9, no?

#6 @scribu
17 years ago

  • Version 2.82.7

#7 @scribu
17 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
17 years ago

It can definitely wait, yes. :-)

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

#9 @scribu
17 years ago

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

#10 @Denis-de-Bernardy
17 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
17 years ago

  • Resolutionfixed
  • Status newclosed

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