0

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

0

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 Apr 7 '15 at 15:05
  • Thus far I haven't. – Devon Hicks Apr 7 '15 at 16:17
0

Solution: Shadow Taxonomy Lib

https://github.com/mrbobbybryant/shadow-taxonomy

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.

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.