Make WordPress Core

Opened 16 years ago

Closed 16 years ago

#12443 closed enhancement (fixed)

General get_ancestors() function

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

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 16 years ago.
get_ancestors.2.12443.diff (1.6 KB ) - added by filosofo 16 years ago.

Download all attachments as: .zip

Change History (16)

#1 @filosofo
16 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
16 years ago

  • Milestone 3.0Future Release

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

#4 @scribu
16 years ago

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

Also see #12908

#5 @voyagerfan5761
16 years ago

  • Cc WordPress@… added

#6 follow-up: @filosofo
16 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
16 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
16 years ago

  • Milestone Future Release3.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
16 years ago

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

#10 @ocean90
16 years ago

  • Keywords needs-refresh added; has-patch removed

#11 @scribu
16 years ago

Related: #8050

#12 @filosofo
16 years ago

  • Keywords has-patch added; get_ancestors needs-refresh removed
  • Milestone Awaiting Triage3.1

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

#13 @kevinB
16 years ago

  • Cc kevinB added

#14 @scribu
16 years ago

  • Resolutionfixed
  • Status newclosed
Note: See TracTickets for help on using tickets.