Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#22882 closed defect (bug) (fixed)

get_post_ancestors() now returns false in addition to array()

Reported by: dd32 Owned by: nacin
Milestone: 3.5.1 Priority: normal
Severity: normal Version: 3.5
Component: Posts, Post Types Keywords: has-patch commit needs-unit-tests
Focuses: Cc:


Under 3.4, get_post_ancestors() would always return an array, in error conditions, it would return an empty array.
Under 3.5, get_post_ancestors() returns an array of ancestors, as in 3.4, except, in the case where the $post passed in is NULL/FALSE/0/empty, then it now returns false.

This results in warning such as this: Warning: Invalid argument supplied for foreach() when the result of get_post_ancestors() was used directly in a loop,

Admitably, it's bad code which triggers this, calling get_post_ancestors() with the global $post object before it's set up in this case, but it's still a chance in behaviour which I felt needs documenting on trac. This is either a instant 'We should just return an array()' or 'wontfix: Plugins are doing it wrong and should fix their code' ticket.

This was found by debugging http://wordpress.org/support/topic/does-wordpress-35-still-support-is_tree?

Attachments (1)

22882.diff (306 bytes) - added by dannydehaan 3 years ago.

Download all attachments as: .zip

Change History (11)

#1 @dd32
3 years ago

Milestone set purely for "It's a 3.5.1 thing, or we don't do it at all"

#2 @aaroncampbell
3 years ago

I think returning array() here is a simple fix and I don't really see a downside.

3 years ago


#3 @dannydehaan
3 years ago

  • Keywords has-patch reporter-feedback added

I've made a simple patch that returns an array when the variable $post isn't available.

#4 @nacin
3 years ago

  • Keywords reporter-feedback removed

#5 @nacin
3 years ago

  • Component changed from General to Post Types
  • Keywords commit needs-unit-tests added
  • Version set to 3.5

#6 @georgestephanis
3 years ago

22882.diff +1

#7 @nacin
3 years ago

In 1177/tests:

Test that get_post_ancestors() returns an empty array when passed a value that evaluates to false. see #22882.

#8 @nacin
3 years ago

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

In 23212:

Always return arrays from get_post_ancestors(). Return an array when passed a value that evaluates to false.

props dannydehaan.
fixes #22882 for trunk.
Test: [1177/tests].

#9 @nacin
3 years ago

In 23213:

Always return arrays from get_post_ancestors(). Return an array when passed a value that evaluates to false.

Merges [23212] to the 3.5 branch.

props dannydehaan.
fixes #22882.

#10 @SergeyBiryukov
3 years ago

#23201 was marked as a duplicate.

Note: See TracTickets for help on using tickets.