WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#12443 closed enhancement (fixed)

General get_ancestors() function

Reported by: filosofo Owned by: filosofo
Milestone: 3.1 Priority: normal
Severity: normal Version: 3.0
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

get_ancestors([object_id], [object_type]) returns an array of the ancestor objects, starting from closest in hierarchy to highest.

This would be helpful for determining general inheritance in themes and plugins.

For example:

if ( in_array(123, get_ancestors(456, 'page') ) ) {
   // highlight a menu item, or query a particular set of objects
}

Attachments (2)

get_ancestors.12443.diff (1.3 KB) - added by filosofo 7 years ago.
get_ancestors.2.12443.diff (1.6 KB) - added by filosofo 7 years ago.

Download all attachments as: .zip

Change History (16)

#1 @filosofo
7 years ago

I have unit tests for this, and I need to hit it a little more, but I wanted to get it in before feature freeze. I'll update shortly, once I have a few minutes to breathe.

#3 @nacin
7 years ago

  • Milestone changed from 3.0 to Future Release

Let's get some unit tests? Punting due to feature freeze. Good for 3.1 IMO. Future for now.

#4 @scribu
7 years ago

The proposed implementation of get_ancestors() will generate a lot of queries when you have deep nesting.

Also see #12908

#5 @voyagerfan5761
7 years ago

  • Cc WordPress@… added

#6 follow-up: @filosofo
7 years ago

Replying to scribu:

The proposed implementation of get_ancestors() will generate a lot of queries when you have deep nesting.

How deep are you talking about, and how would that happen? I've never seen so many levels in categories that it would make a difference (in other words, more than a handful).

Also the object cache should help with that.

#7 @filosofo
7 years ago

Two more thoughts about that:

  • If this is a live scenario, it would be a good use for MPTT
  • It probably does interesting things to that taxonomy's admin, too.

#8 in reply to: ↑ 6 @scribu
7 years ago

  • Milestone changed from Future Release to 3.1

Replying to filosofo:

How deep are you talking about, and how would that happen? I've never seen so many levels in categories that it would make a difference (in other words, more than a handful).

You're probably right. The speed gained by having one query instead of 5 (for example) wouldn't justify the increased memory use.

#9 @scribu
7 years ago

By the way, _get_post_ancestors() is redundantly called each time for get_post().

#10 @ocean90
7 years ago

  • Keywords needs-refresh added; has-patch removed

#11 @scribu
7 years ago

Related: #8050

#12 @filosofo
7 years ago

  • Keywords has-patch added; get_ancestors needs-refresh removed
  • Milestone changed from Awaiting Triage to 3.1

get_ancestors.2.12443.diff refreshes patch, avoids infinite loops, and takes advantage of _get_post_ancestors's work, if available.

#13 @kevinB
7 years ago

  • Cc kevinB added

#14 @scribu
7 years ago

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.