I have written the following function which copies all post terms from the "tribe_events_cat" taxonomy to the "categoria" taxonomy when the post is saved. There is a bug where in order for the terms to be copied, I need to click "update" twice (i.e. save the post twice).
I believe this happens because when I call get_the_terms, the post has not been saved yet.
Is there any way around that, so that get_the_terms gets the terms from the newly updated post?
function bam_save_event_cat( $post_id ) {
$taxonomy = 'categoria';
$tribe_cats = get_the_terms( $post_id, 'tribe_events_cat');
foreach($tribe_cats as $tribe_cat) {
if( empty($tribe_cat->name) ) continue;
$catname = $tribe_cat->name;
$cats[] = $catname;
}
wp_set_object_terms( $post_id, $cats, $taxonomy );
}
function bam_save_event($post_id) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
if(get_post_type( $post_id ) == 'tribe_events' ) {
remove_action( 'save_post', 'bam_save_event' );
wp_update_post( array( 'ID' => $post_id ) );
add_action( 'save_post', 'bam_save_event' );
bam_save_event_cat( $post_id );
}
}
add_action( 'save_post', 'bam_save_event' );