0

I've got a custom post type 'articles' and I've added a custom taxonomy 'articles_categories' which has its own terms.

I was under the impression you can't have categories and tags on custom post types? But I can't see how Taxonomies and Terms are the same? And if you can have categories and tags on custom post types what is the reasoning for using custom Taxonomies? It's all very confusing.

Could someone explain the context of taxonomies and why can't you use categories and tags on custom post types?

Many thanks in advance

1 Answer 1

1

I was under the impression you can't have categories and tags on custom post types?

You can have categories and tags on custom post types!

But I can't see how Taxonomies and Terms are the same?

But they are the same, Categories are a taxonomy, as are post tags, they're stored in the database the same way as custom taxonomies, and use the same code and user interface.

The part that makes custom taxonomies "custom" is that it wasn't WP Core that registered them, it was you/a plugin.

Having said that, categories and tags have some legacy baggage that shows in certain file names, and some category/tag specific APIs. For the most part these can be ignored, and they have no bearing on custom taxonomies. Custom post types are in a very similar boat

And if you can have categories and tags on custom post types what is the reasoning for using custom Taxonomies? It's all very confusing.

There's a few reasons:

  • What if I want to show different categories of a product post type with just the products?
  • What if I want to list all categories next to posts, but some of those categories have nothing to do with posts because they're only used for a custom post type?
  • What if I want a taxonomy that is not a category or tag? E.g. country? Colour? Size?

A taxonomy is just a way to filter/group things together. Taxonomies have terms that are then used as boxes to group them together.

If you want to group X by Y, Y is a taxonomy, and X is a post type.

E.g. I want to group products by colour.

An example of a colour would be red, green, yellow, these are terms in the colour taxonomy.

Terms can be hierarchical, or non-hierarchical. E.g. Categories versus tags. I could extend the colour taxonomy example to say it is hierarchical, and that the yellow term has child terms for goldenrod or banana yellow.

So any time that you want to group posts by something, that's what taxonomies are for.

Also, anytime you need to filter/include/exclude posts by something, use a taxonomy. Some people store a post meta flag and then do a meta query, incurring a disastrous performance penalty. Just use a taxonomy instead, e.g. a show_on_homepage taxonomy with a yes and a no term, can be hundreds of times faster than a custom field.

1
  • Thanks Tom. The reason I was getting confused was that when adding custom taxonomies I didn't realise you can differentiate between categories and tags by whether or not you set 'hierarchical' => true when registering the taxonomy. I couldn't see how to get the equivalent of a 'tag' so to speak.
    – pjk_ok
    Commented May 18, 2020 at 18:27

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.