5

I'm looking at the get_categories() function in the WordPress codex to see how I can return all of the children of each "root parent" ( categories that have no parents ). For example, if I am interested in the children for multiple "root parents", I'd like to know if there will be more than one instance of the same child under a different "root parent".

So, this leads me to ask:

Can child categories in WordPress have multiple, different parents?

6

No, a term can have many children, but only a single parent. The parent field in the database can hold only a single value.

It is a hierarchical one to many relationship, not a many to many relationship.

The same is true of posts. A post can have only 1 parent, but many children. To get around that you would use taxonomies.

A sidenote on wp_term_taxonomy, and Hard Proof

It's been suggested that the same term could have multiple entries in this table allowing it to have multiple parents and be shared across multiple parent terms.

Here is the database schema:

enter image description here

As you can see if the same term and taxonomy were referred to multiple times, but with a different parent, perhaps a term can have multiple parents?

No, the term_id and taxonomy_id are both unique, such a thing would not be possible to create in that table without changing its schema. To demonstrate this, I opened up sequel pro.

Here we have a term in my local copy of my blog:

enter image description here

It has a parent of 4, lets attempt to give it a second parent of 5:

INSERT INTO `tjn2_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`)
VALUES
    (13, 13, 'category', '', 5, 0);

When ran this query gives the following error:

enter image description here

So no, a term cannot have more than one parent. But even if it could, there would be no mechanism in the APIs to find out short of raw SQL statements, so such a term would be useless and unusable in the frontend and backend UIs

  • 2
    There is a subtlety that one needs to be careful about here. You must specify the child by taxonomy id because the same term may occur in multiple places in a hierarchical taxonomy. In other words, a term considered as a string can have multiple parents but that term actually is not one taxonomy item but corresponds to multiple taxonomy items with the same string value. – user27457 Aug 15 '14 at 21:11
  • You mean if a tag and a category share the same slug? Sure I can see that, though if you're all within the same taxonomy in the OP's scenario it shouldn't be an issue. Your point also applies to post types too! – Tom J Nowell Aug 15 '14 at 21:18
  • In a heirarchical taxonomy the same term can occur in multiple places. Consider the hierarchy on cars with first level - GM, Ford, Chrysler. At the second level they could each have the sub-category sedan, truck I.e., GM->sedan and Ford->sedan where "sedan" would be the same term but not the same taxonomy term. If you look at the structure of the WordPress databases there is a wp_terms and a wp_term_taxonomy table to allow for sharing of terms. Posts are structured different. It uses a single table with a post_parent field. Also, the sharing is in the same taxonomy. – user27457 Aug 15 '14 at 21:36
  • In other words, there is a reason why there is a term_id and term_taxonomy_id. Note that the wp_posts table only has an ID column so it really is structured differently. – user27457 Aug 15 '14 at 21:43
  • 1
    No, there's no way of setting that up in the UI, or the API, and those fields are unique in the database schema, go try the SQL and it will fail – Tom J Nowell Aug 15 '14 at 22:39
0

Try to structure your categories with the tax-meta-class http://en.bainternet.info/wordpress-taxonomies-extra-fields-the-easy-way/.

You will be able to add meta fields to your taxonomies (categories, tags, custom tax).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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