Here’s an (untested) example on how to get the saved post’s categories, check if any of them exists in another taxonomy, and terms that exists to the post. Instead of the generic save_post
action you can use the post type specific one, save_post_{post_type}
. If you want to prevent the check from running every time the post is saved / updated, you can utilize the third parameter the hook provides, which tells you is the action for a new or an existing post.
// hook to post type specific save action
add_action( 'save_post_post', 'studio_code_save_post', 10, 3 );
function studio_code_save_post( $post_ID, $post, $update ) {
if ( wp_is_post_revision( $post ) ) {
return;
}
// uncomment to prevent code running on post update
// if ( $update ) {
// return;
// }
$categories = get_the_terms( $post, 'category' );
$studio_terms = array();
if ( $categories && is_array($categories) ) {
foreach ($categories as $category) {
// check, if the term exists in another taxonomy
if ( term_exists( $category->slug, 'studio' ) ) {
$studio_terms[] = $category->slug;
}
}
}
if ( $studio_terms ) {
// append terms to post
wp_set_object_terms( $post_ID, $studio_terms, 'studio', true );
}
}