Support

Account

Home Forums General Issues update_field after create post by code

Solved

update_field after create post by code

  • Hi

    i have question about update_field after creating a post by code (importing from xlsx)

    I have some acf fields (users and taxonomies) in a custom post type “match”, so i have winners, loosers, and a multiple taxonomy select

    i create post like this

    $postarr = array(
    'post_title' => sanitize_text_field( wp_unslash( $titolo_post ) ),
    'post_status' => "publish",
    'post_type' => "fight",
    );
    $post_id = wp_insert_post( $postarr);	

    and then i try to update fields

    update_field("match_valevole_per_titolo",$id_titoli,$post_id);
    update_field("vincitore",$lista_vincitori,$post_id);

    where $id_titoli is an array with taxonomy ids, and $lista_vincitori is array with users ids

    In the backend, if i enter in the post, i see all fields correctly, but quering posts there is something that is wrong BUT it is fixed if i save post (witout modify any field) from backend….just like saving updates something…. what i’m doing wrong?

  • I have a feeling its due to how the data is inserted. Something in my head tells me that you need change the priority on the save post function when inserting data from an import.

    I assume the post is created when you import from your XLS?

    What does your function look like to import and add the ACF fields?

    Check the docs as I think you need to amend the priority on the function. You may need to use the field key rather than the field name but think its the priority.

  • i think is about how data is inserted but not about priority. I create post and i have its ID, then i update field. I think problem is about serialized data and how i have to manage them before insert

  • i think i fixed with

    update_field(“match_valevole_per_titolo”,serialize($id_titoli),$post_id);

  • i fixed in this way

    .....
    $id_titoli[] = "$term_id"; // before was $id_titoli[] = $term_id; ===>>> the error was here
    .......
    $postarr = array(
    'post_title' => sanitize_text_field( wp_unslash( $titolo_post ) ),
    'post_status' => "publish",
    'post_type' => "fight",
    );
    $post_id = wp_insert_post( $postarr);
    .......
    update_field(“match_valevole_per_titolo”,$id_titoli,$post_id);
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.