WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 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:

Description

It should return an empty array instead.

Attachments (2)

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

Download all attachments as: .zip

Change History (13)

comment:1 Denis-de-Bernardy5 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',
	));

comment:2 Denis-de-Bernardy5 years ago

on a non-image attachment page, even.

comment:3 Denis-de-Bernardy5 years ago

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

Denis-de-Bernardy5 years ago

comment:4 Denis-de-Bernardy5 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()

comment:5 scribu5 years ago

  • Milestone changed from 2.8 to 2.9

This can wait until 2.9, no?

comment:6 scribu5 years ago

  • Version changed from 2.8 to 2.7

comment:7 scribu5 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) ).

comment:8 Denis-de-Bernardy5 years ago

It can definitely wait, yes. :-)

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

comment:9 scribu5 years ago

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

comment:10 Denis-de-Bernardy5 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)

Denis-de-Bernardy5 years ago

comment:11 markjaquith5 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.