Support

Account

Home Forums General Issues Get_fields() returns false until I update the post Reply To: Get_fields() returns false until I update the post

  • BTW, I will have the post id because i am doing wp_insert_post which returns the newly created post.

    The data I am currently using for testing with acf_maybe_get_field is data that was already imported, but not imported using the field_key. It was just using the wp_insert_post and the field names. This data when I use get_fields() returns null.

    In my new importer, I’m going to do like the example shows on the link you provided. I need to test this, but i was thinking something like this:

    function helper_get_field_key($field_name, $post_id){
      $maybe_field_obj = acf_maybe_get_field( $field_name, $post_id, false );
    
      return $maybe_field_obj['key'];      
    }
    
    // Create new post.
    $post_data = array(
        'post_title'    => 'My post',
        'post_type'     => 'my-post-type',
        'post_status'   => 'publish'
    );
    $post_id = wp_insert_post( $post_data );
    
    // Save a basic text value.
    $get_field_key_email = helper_get_field_key('email', $post_id);
    $value = "[email protected]";
    update_field( $get_field_key_email, $value, $post_id );

    I think something like this will work, i haven’t tested this exact scenario yet. I will today though and i can report back.