Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
share|improve this question

1 Answer

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' );
share|improve this answer
I had my fingers crossed but no, it didn't work at all. Thanks anyway though! – j-man86 Jun 13 '12 at 7:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.