0
votes

The WordPress codex has a lot of examples of how to register custom taxonomies with custom post types, but I couldn't find much about using built-in taxonomies (tags & categories) with cpts.

I have a cpt called listings, and I need to add the standard category and tag UI elements to the listing cpt page. I also need to do this with code in my functions.php, rather than using a plugin.

2

2 Answers

1
votes

Not a problem at all. When you register the post type, just add this argument to the array:

'taxonomies' => array( 'category', 'post_tag' )
0
votes

Suppose you defined your cpt (custom post type) by the following:

register_post_type('listings', $args); // where $args is an array of your cpt settings

Then you could use the following to add taxonomy:

// category-like:
register_taxonomy('listing_category', array('listings'), array('hierarchical' => true, ...));

// tag-like:
register_taxonomy('listing_tag', array('listings'), array('hierarchical' => false, ...);

In fact, I personally put those custom type definitions in my own plugin (not open for public as it provide my own site functionalities, which obviously not suit the others at all).

The problem of putting in functions.php increases the difficulty to change to a new theme (although changing theme is not so often, but for self-owned blog, it do happen in some day).

Moreover, the custom post types should be site-wide, not depending on the current theme. So semantically it should not be in the theme's directory.