• Hi there.
    I have a fairly complicated structure and I need some ideas about how to plan the relations between the CTs.

    I have some Products(CT1) which contain Herbs(CT2) and these herbs are good for Diseases(CT3).
    So far they are related as they have CPT-onomy attached to each other. I use CT for all because I need to have single pages for each.

    Products (CT1) have Herbs and Diseases attached.
    Herbs (CT2) have Diseases attached.
    Diseases (CT3) have Herbs attached.

    I need to find a solution about how to cross-update them in order to update each other automatically, so when I create Disease 1 and select Herb 1 for it, the Herbs (CT2) should be also updated, so that Herb 1 will have Disease 1 attached.
    Normally it would also update the Products CT1, so that Product 1 would have both Disese 1 and Herb 1 attached.

    Does this make any sense?

    Thanks in advance.
    Cheers,
    Attila

    https://www.ads-software.com/plugins/cpt-onomies/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Did you find an answer to this? I have a similar need.

    Bump. I need the same thing as well.

    Ok I’m working on a solution. This is what I have so far…This is a multiple way association where when you create/edit a post of a certain CPT-onomy (CPT #1) with associations to particular terms of your other CPT-onomies (CPT #2-infinity), it will update each of the associated terms with a connection back to the post.

    For example:

    CURRENT POST (CPT #1)
          tagged with
             CPT #2 TERM #1
             CPT #3 TERM #2
             CPT #4 TERM #3

    Will update:

    CPT #2 TERM #1
          tagged with
             CURRENT POST
    CPT #3 TERM #2
          tagged with
             CURRENT POST
    CPT #4 TERM #3
          tagged with
             CURRENT POST

    WHAT IT WILL NOT DO:

    1.) If CPT #3 is associated with CPT #2 and your current post is tagged with CPT #3, it will NOT recursively update the current post to be associated with all CPT #2 terms that are associated with the CPT #3 terms selected.

    2.) If CPT #2 is hierarchical and TERM #3 belongs to TERM #2 which belongs to TERM #1, tagging the current post with TERM #3 will not connect it to TERM #2 and TERM #1

    SOMEONE PLEASE FEEL FREE TO ADD THOSE CAPABILITIES, I’ll work on it more when I can.

    function cross_update_CPTonomies($post_id) {
        // GET REAL POST ID IF THIS IS A REVISION
        if($parent_id = wp_is_post_revision($post_id)){
            $post_id = $parent_id;
        }
    
        $post_type = get_post_type($post_id);
        global $cpt_onomy;
    
        // WHAT CPT-ONOMIES ARE CONNECTED?
        $CPTonomies = array({CPT #1 NAME}, {CPT #2 NAME}, {CPT #3 NAME});
    
        // CHECK IF CURRENT POST IS ONE OF THE CONNECTED CPT-ONOMIES
        if(in_array($post_type, $CPTonomies)){
    
            // REMOVE THE CURRENT POST TYPE FROM CPT-ONOMIES ARRAY (MEANS YOU CONNECT THE POST TO THE REMAINING CPT-ONOMIES AND NOT TO CURRENT POST TYPE)
            // THIS IS OPTIONAL IF YOUR CPT-ONOMIES CONNECT TO THEMSELF
            if(($key = array_search($post_type, $CPTonomies)) !== false) {
                unset($CPTonomies[$key]);
            }
    
            // LOOP THROUGH EACH REMAINING CPT-ONOMY AND CONNECT IT TO CURRENT POST
            foreach($CPTonomies AS $CPTonomie){
                $terms = get_the_terms($post_id, $CPTonomie);
    
                // CHECK IF POST HAS BEEN ASSOCIATED WITH ANY TERMS OF THIS CPT-ONOMY
                if($terms && !is_wp_error($terms)){
    
                    // FOR EACH TERM THAT WAS ASSOCIATED, LINK FROM THAT TERM BACK TO CURRENT POST
                    foreach($terms as $term){
                        $cpt_onomy->wp_set_object_terms($term->term_id, $post_id, $post_type, true);
                    } // END FOR EACH TERM
                } // END IF POST HAS BEEN ASSOCIATED WITH TERMS OF THIS CPT-ONOMY AND THERE ARE NO ERRORS
            } // END FOR EACH REMAINING CPT-ONOMY
        } // END IF POST TYPE IS ONE OF THE CONNECT CPT-ONOMIES
    
    } // END CROSS UPDATE CPT-ONOMIES
    
    add_action('save_post', 'cross_update_CPTonomies');
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Cross update taxonomies’ is closed to new replies.