Opened 3 years ago
Closed 3 years ago
#12443 closed enhancement (fixed)
General get_ancestors() function
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 3.1 |
| Component: | Taxonomy | Version: | 3.0 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | WordPress@…, kevinB |
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)
Change History (16)
+1
- 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.
The proposed implementation of get_ancestors() will generate a lot of queries when you have deep nesting.
Also see #12908
comment:5
voyagerfan5761 — 3 years ago
- Cc WordPress@… added
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.
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.
- 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.
By the way, _get_post_ancestors() is redundantly called each time for get_post().
comment:10
ocean90 — 3 years ago
- Keywords needs-refresh added; has-patch removed
comment:11
scribu — 3 years ago
Related: #8050
comment:12
filosofo — 3 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.
comment:13
kevinB — 3 years ago
- Cc kevinB added

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.