Support

Account

Home Forums General Issues Saving data with update_field not updating user field in backend

Solved

Saving data with update_field not updating user field in backend

  • I’m creating users and custom posts via a csv. On the whole it works well apart from saving user data to the user field. No matter what I put in, when I look at the custom post all the other data is saved apart from the user field, which just appears empty.

    Where am I going wrong?

    $email_address = $data[4];
            if( null == username_exists( $email_address ) ) {
    
              // Generate the password and create the user
              //$password = wp_generate_password( 12, false );
              $password = "password";
              $user_id = wp_create_user( $email_address, $password, $email_address );
    
              // Set the nickname
              wp_update_user(
                array(
                  'ID'          =>    $user_id,
                  'nickname'    =>    $data[3],
                )
              );
    
              // Set the role
              $user = new WP_User( $user_id );
              $user->set_role( 'regional' );
    
              $field_key = "field_5a1ee1c1c3463";
      				$value = array($user_id);
      				update_field( $field_key, $value, $post_id );
    
              // Email the user
              //wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );
    
            } elseif(username_exists( $email_address )){
              $user = get_user_by( 'email', $email_address );
              $user_id = $user->ID;
    
              $field_key = "field_5a1ee1c1c3463";
      				$value = array($user_id);
      				update_field( $field_key, $value, $post_id );
              echo the_field('field_5a1ee1c1c3463');
    
            }
  • I’ve looked over your code and I don’t see anything wrong with it, given what I see.

    Are you interacting with this field before the code you’ve given? Are you using get_post_meta() or any function that would cause WP to query the meta values for this field?

    If you are you could be having issues with the WP meta cache. Try clearing the cache before the above code

    
    wp_cache_delete($post_id, 'post_meta');
    
  • Thanks John… although that didn’t solve my problem it did make me realise the order I had things.

    I had the whole users creation section above $post_id = wp_insert_post( $my_post ); meaning it missed that part out.

    All working now.

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

The topic ‘Saving data with update_field not updating user field in backend’ is closed to new replies.