Opened 3 years ago

Closed 3 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: 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)

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

Download all attachments as: .zip

Change History (16)

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.

  • 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

  • Cc WordPress@… added

comment:6 follow-up: ↓ 8   filosofo3 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.

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.

comment:8 in reply to: ↑ 6   scribu3 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.

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

  • Keywords needs-refresh added; has-patch removed

Related: #8050

  • 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.

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