Support

Account

Home Forums Front-end Issues Update post field

Solved

Update post field

  • Hy,

    I am using the ACF taxonomy field in a front end form, to populate the dropdown select box with the custom taxonomies i created earlier.

    The problem is that the taxonomy gets saved in the form, but not in the side bar.

    Please see this image : [removed]

    In functions.php i have added the code :

    function my_post_title_updater( $post_id ) {

    if ( get_post_type( $post_id ) == ‘item’ ) {

    $my_post = array();
    $my_post[‘ID’] = $post_id;
    $my_post[‘post_title’] = get_field( ‘titlu’, $post_id );
    $my_post[‘post_name’] = get_field( ‘titlu’, $post_id );
    $my_post[‘post_content’] = get_field( ‘descriere’, $post_id );
    $my_post[‘tax_input[cat]’] = get_field( ‘categorie’, $post_id );

    wp_update_post( $my_post );

    }

    }

    // run after ACF saves the $_POST[‘fields’] data
    add_action(‘acf/save_post’, ‘my_post_title_updater’, 20);

    But it is not working

    My plan is to have the user submit the form on front end, and thenset the CATEGORIE taxonomie in the form field, and in the sidebar.

    My taxonomie name is ‘cat’ and the field is called ‘categorie’

    Can anyone help me?

    Thnak you!

  • It should look something like this

    
    $my_post['tax_input'] = array(
      'category' => array(
        get_field('categorie', $post_id)
      )
    );
    

    see https://developer.wordpress.org/reference/functions/wp_insert_post/. Each element of ‘tax_input’ is an array. The key is the taxonomy name and the value is an array of term IDs.

    Although I’m not 100% sure that tax_input works with wp_update_post. You may need to use wp_set_object_terms() https://developer.wordpress.org/reference/functions/wp_set_object_terms/

  • You are awsome!!

    Thank you verry much!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.