0

I was looking for a function to get all ancestors of a post or category and found get_ancestors( $object_id, $object_type ). To my surprise, the Codex shows that the second parameter is required:

$object_type
        (string) (required) The name of the object type (page, hierarchical post type, category, or hierarchical taxonomy) in question

    Default: None 

Aren't post, page and category IDs all unique to each other, i.e. the intersection of their sets would be empty?

If not, why not? It only seems to complicate an API if you have to specify the object type given that posts, pages, and categories are all data objects of a single CMS.

1 Answer 1

1

There are multiple types of data objects:

  • posts: pages, regular posts, nav menu items, attachments and custom post types
  • terms for different taxonomies: categories, tags, nav menus
  • comments
  • links
  • options
  • various meta data

Term ids are for a different type of objects than post ids. Both are primary keys for different tables. From Potential roadmap for taxonomy meta and post relationships:

At the moment, terms are represented in WordPress using two different IDs: a term ID, and a term taxonomy ID. A term ID (and name and slug) can actually appear in multiple taxonomies, so to identify a particular term, you must have either the term ID and the corresponding taxonomy, or just the term taxonomy ID. This dates back to the original taxonomy schema in WordPress 2.3.

get_ancestors() is one of those functions that are Doing Too Much™. If you want just post (page, attachment) ancestors, use get_post_ancestors($object_id).

If not, why not? It only seems to complicate an API if you have to specify the object type given that posts, pages, and categories are all data objects of a single CMS.

The specific date for these objects are very different. If you look at the posts table, you will see many fields which aren’t needed for terms, plus revisions, drafts etc. Keeping those in different tables makes it easier to create efficient table indexes, even custom indexes, depending on your actual usage of WordPress.

4
  • So within the "terms" group, there may be duplication in term IDs, but all terms have a unique term taxonomy ID? What is the purpose of a term ID for a term if the term taxonomy ID is sufficient to uniquely identify it, and even across multiple term types? Can you reference anything official that differentiates post ids from term ids. I found [wordpress.org/support/topic/… response) in Wordpress Support, but there is no evidence for the claim.
    – bsy-web
    Commented Jul 30, 2014 at 7:50
  • @bsy-web Both are primary keys for different tables. I have added that to my answer. A look at the table structure is as “official” as it gets.
    – fuxia
    Commented Jul 30, 2014 at 8:38
  • I don't know much about databases, but if primary keys can be created with a constraint to be unique between multiple tables' primary keys, then every data object would have a unique ID within its own data type and between all other (CMS) data types. You answered the main question. Any pointers towards the second part of the question: "If not, why not?"
    – bsy-web
    Commented Jul 30, 2014 at 9:33
  • @bsy-web I made another update, explained the optimization for indexes.
    – fuxia
    Commented Jul 30, 2014 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.