Support

Account

Home Forums Front-end Issues Taxonomies are not saved via the frontend. But it works in the backend. Why?

Solved

Taxonomies are not saved via the frontend. But it works in the backend. Why?

  • I would like to use this function to activate my taxonomies via a true/false field. And it works perfectly in the backend.

    function update_post_taxonomy( $post_id ) {
    $myfield = get_field('my_field_name');
    if( $myfield ) {
    wp_set_object_terms($post_id, 'myterm', 'mytaxonomy', true);
    } else if ( empty( $myfield ) ) { 
    wp_remove_object_terms( $post_id, 'myterm', 'mytaxonomy', true );
    }
    }
    add_action( 'acf/save_post', 'update_post_taxonomy', 10 );
    

    But unfortunately not in the frontend via acf_form().
    Could someone help me here? Thanks!

  • And once again I answer myself 😉
    I forgot the $post_id inside the acf-field. This is how it works perfectly:

    function update_post_taxonomy( $post_id ) {
    $myfield = get_field('my_field_name', $post_id);
    if( $myfield ) {
    wp_set_object_terms($post_id, 'myterm', 'mytaxonomy', true);
    } else if ( empty( $myfield ) ) { 
    wp_remove_object_terms( $post_id, 'myterm', 'mytaxonomy', true );
    }
    }
    add_action( 'acf/save_post', 'update_post_taxonomy', 10 );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.