As part of a much broader problem I'm trying to tackle, I am wondering if I can automatically create a taxonomy term with the same name as a new post of a specific CPT.

The idea would be as follows. I'd have a cartoon-series CPT. When I create a new cartoon series post, a taxonomy term for the cartoon-series taxonomy would be created with the same name as this new post. Along with that top-level term, two sub-terms would be created: "Episodes" and "Special Features"

So, for each new cartoon series post I create, I'd get a total of three terms added. One with the same name as the post and two under that, that would always be the same for all cartoon series.

A few potential problems I foresee right away:
Can I have terms in a taxonomy that are exactly the same? Like Episodes appearing multiple times as sub-terms.
Can a taxonomy have the same name as a CPT? Ideally, both would be named cartoon-series. The slugs for the sub-terms would need to be the same as I'd be displaying them in the url.

GOOD domain/cartoon-series/<series-name1>/episodes/<episode-name>   
GOOD domain/cartoon-series/<series-name2>/episodes/<episode-name>  
BAD  domain/cartoon-series/<series-name2>/episodes-1/<episode-name>

"episodes" would need to be the same slug even though they are not the same term. I'm not sure if this is possible even if they are under different top-level terms.

All of this is in effort to solve this problem: Need help with complex custom post type setup

I'm all ears if you have any other ideas!

Thanks! Matt

  • Quick comment. You don't want your term name to be your post_name or post_title. Why? Because the post name and title can change and your taxonomy term could break. Instead use the post_id as the term name. Then for the rewrite rules and frontend displays uses the term name to find the post name or title.
    – Slam
    Commented Jan 31, 2020 at 6:08

2 Answers 2


Solution: Shadow Taxonomy Lib


This looks like a robust solution for this issue and including wp cli support for creating, deleting, and evaluating the linkages.

I haven't test it yet but it seems like the perfect solution for this issue.


I recently ran into a similar situation. While I can't currently remember the source, the code below will create a new entry into your taxonomy of choice, it could serve as the base for your needs.

// Automatically creates a category in the case log and adds it to the post.    
function add_title_as_category( $postid ) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    $post = get_post($postid);

    if ( $post->post_type == 'case') { // change 'post' to any cpt you want to target
        $term = get_term_by('slug', $post->post_name, 'case-log');

        if ( empty($term) ) {
            $add = wp_insert_term( $post->post_title, 'case-log', array('slug'=> $post->post_name) );
            if ( is_array($add) && isset($add['term_id']) ) {
                 wp_set_object_terms($postid, $add['term_id'], 'case-log', true );

add_action('save_post', 'add_title_as_category');
  • Have you run into any problems with terms having the same slug?
    – Mattaton
    Commented Apr 7, 2015 at 15:05
  • Thus far I haven't. Commented Apr 7, 2015 at 16:17

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.