1

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' );
2

This is a stab in the dark, but have you tried using the set_object_terms hook for your bam_save_event_cat function?

function bam_save_event_cat( $post_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
    $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' );

        add_action( 'set_object_terms', 'bam_save_event_cat', 10, 6 );
    }
}

add_action( 'save_post', 'bam_save_event' );
  • I had my fingers crossed but no, it didn't work at all. Thanks anyway though! – HandiworkNYC.com Jun 13 '12 at 7:41
  • This is a great tip, and it works. Especially helpful if you're trying to intercept a change to a WooCommerce product's shipping class (which is saved as taxonomy terms) – Avishai Jun 19 '17 at 13:26
1

I think that I have encountered the same problem.

You have to set a higher priority than the default of 10 to your save_post action. The priority is specified as the third argument when adding an action as explained in the Codex

Like :

add_action( 'save_post', 'bam_save_event', 99 );

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.