1

I have multiple customs post types:

CPT1 = "bike",

CPT2 = "car"

They share a common custom taxonomy "colors" (with terms : "blue", "red" ).

They also have their own taxonomy with terms like : "blue_bike", "red_bike" ..

I'm searching how to auto update the post taxonomy with the custom taxonomy.

For example : If i only select the custom taxonomy term "blue" in the Custom Post Type "Bike".

Is it possible when i save the post, it will automatically update the post with the term "blue_bike" ?

And if it's possible how to do that ?

I'm trying for the past couple hours with the function wp_set_object_terms, with no result (my php skills are not so good)

1

Yes it is. But you have to know the exact taxonomy's slug, since colors are not like preserved terms to be generated programmatically. Here is an example of how to do it:

// run our function when a post is published
add_action('save_post','update_my_taxonomies');
function update_my_taxonomies($post_id){
    // Check if the post has a particular taxonomy
    if(has_term( 'blue', 'colors', $post_id )){
        // Assign a term to our post
        wp_set_object_terms( $post_id, 'blue', 'blue_bike' );
    }
}

This will set the blue for the blue_bike if the post has the blue term as its color taxonomy.

  • I follow your advice and i have defined all my taxonomy (terms and slugs) and now it works like a charm ! Thanks a lot – julien.b Jun 21 '17 at 7:24

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.