Allow Child Category To Have Multiple Parents

  1. Vlad Lasky


    There should be an option to allow child categories to belong to multiple parents.

    I outlined an example use for this feature at the end of this previous trac issue: http://core.trac.wordpress.org/ticket/12898

    Let's say I am geotargeting, and I have identified that the user is from Germany. I can then display posts from the category 'Germany'.

    But if I can make Germany a child category of two parent categories - 'European_Union' and 'German_Lanugage_Articles', I would then be able to easily display additional posts from these categories that are also relevant to German visitors.

    At the moment, WordPress would only allow Germany to be a member of one of these parent categories, but not both.

    I do not know of any ways to neatly acheve this without creating a custom MySQL table and code to support these multiple parent relationships.

    Posted: 8 years ago #
  2. Vlad Lasky


    If this helps, I am prepared to sponsor the implementation of this enhancement.

    If you're a core developer, shoot me a message and name your price.

    Posted: 8 years ago #
  3. Jen
    Key Master

    I think this is probably plugin territory right now.

    Posted: 8 years ago #
  4. Mark


    Taxonomy intersection like this can already be done by using a combination of Categories and Tags (the latter being orthogonal to category organization. A plugin to smooth this process would be good, but I don't think the core needs changing for this.

    Posted: 8 years ago #
  5. Daljo628

    Functionally, this is an impossibility due to the present native structure of the database. I was hoping for feasability too, but I just found a workaround, based on how I structured my architecture. I'd suggest using tags, instead. Long story short, each category is a node in a Tree Data Structure with a pointer to its parent, but no pointers to children. This is for simplicity and ease of use. If they were to also store pointers to children, and modify the structure to permit multiple parents, the structure would become (I believe) exponentially more complicated, and hamper further development on more critical areas.

    For the technical details of how WordPress stores Categories, read on ... for the sake of brevity and clarity, I'll omit a few columns from a few tables, that didn't seem relevant.

    Categories are stored in the WordPress Database across three tables, wp_terms wp_term_taxonomy and wp_term_relationships iirc. wp_terms stores a term_id, term_name, and term_slug. term_id is auto-incrementing, so the code starts by shoving the name and slug into the wp_terms table, and then fetching the ID at which it was inserted. Name doesn't have to be unique, but slug does!

    Then we move onto the wp_term_taxonomy table. As you may have noticed, we've been inserting words, but never saying what they meant! The wp_term_taxonomy table stores term_taxonomy_id, term_id, taxonomy, description, parent, and count. Now, count simply stores (for category purposes) the number of events that are assigned to that category, ignoring child-categories. We can ignore it.

    term_taxonomy_id is the auto incrementing id that bumps up whenever you add in a new taxonomy. term_id is the term in question from the wp_terms table. Notice that this table has no slot for name. It simply links into the wp_terms table, so you can have multiple term_taxonomies, pulling from the same name in wp_terms. taxonomy is what the term is to be considered. For our purposes, it will always be 'category'. description is the description of the category, and parent is an INT, containing the parent category (that really cool thing that we care about).

    wp_term_relationships is simply tying wp_term_taxonomy in with actual object_ids (posts, etc.) It's the link between a post and its category, allowing you to create multiple entries, to link one post to multiple categories (because the link is stored seperately from either the parent or child).

    Now, the significance of all this. Categories are built as a tree data structure. Each category is a very simple construct, storing a payload and a pointer -- the normal, lightest way to build trees. Parents don't have pointers to their children, that could have to store anywhere from {no values} up to {thousands} or more. Instead, each category just stores (data) (parent) and you can travel up and down the tree. To travel to a parent, simply follow the pointer! To travel back down, it becomes a bit more cumbersome, as you have to scan the entire table for any elements that have the parent assigned as a parent -- fortunately, an easy task with a MySQL database, simply querying SELECT * FROMwp_term_taxonomy WHERE parent = '{$term_taxonomy}', where $term_taxonomy is the term_taxonomy of the parent in question!

    Long story short, use tags, because a plugin would have to rewrite the whole system, which is possible, but not very cost-effective!

    Posted: 8 years ago #
  6. Binh


    I need this function as in Drupal.
    No it's not complecated. This is what I done in uni project, and is supposed very simple.

    The following required fields already exist in wp_term_taxonomy

    Term ID | parent ID

    So the tree is already complete. Now what you need is to allow duplicate term ID. That's for example:

    Tax ID | Term ID | parent ID
    10 | 30 | 10
    11 | 30 | 13

    That's for example
    Taxonomy number 10 is a record of Term 30 (T-Shirt), which is a child of Term 10 (Woman Clothing)
    Taxonomy number 11 is a record of Term 30 (T-Shirt), which is a child of Term 13 (Man Clothing)

    No it's not complicated, it's just un-thought.

    And for me, tagging sucks because there is no filter of Category vs Tag. And tag has no hierarchy, it SHOULD.

    Posted: 7 years ago #
  7. Henry


    Has anyone given any thought to Binh's solution. I can't find fault with it. Nice approach

    Posted: 5 years ago #
  8. soultrader67


    The tag workaround is ok but as in life, sometimes there are more parents! Seriously though it does need a workaround other than tagging.

    Posted: 5 years ago #
  9. Ipstenu (Mika Epstein)

    There are also custom taxonomies. So you make one called Languages, and list them all there. You make another called EU and list the nations there.

    Posted: 5 years ago #
  10. Luke Gedeon

    Even with a custom taxonomy the database index keyed on term_id-taxonomy does not allow multiple parents. Honestly all we need to do is change the key requirement on the wp_term_taxonomy table so that we can have multiple rows with the same term_id and taxonomy but different parent and then plugins can do their thing.

    Posted: 5 years ago #

RSS feed for this topic

Reply »

You must log in to post.

  • Rating

    21 Votes
  • Status

    This is plugin territory