Support

Account

Home Forums General Issues New field not saved in database Reply To: New field not saved in database

  • While @gummi’s solution will get you there, I think that it’s always better to code this, especially when dealing with new values. As a developer and programmer it’s not a good programming practice to assume that a value will always be present.

    
    $value = get_field('field_name');
    if (!$value) {
      $value = 'some default value';
    }
    

    Yes, this does mean a little more code, but it’s a lot less work than updating every post on a site to have the default value.

    If you have thousands of posts on a large site there’s a good chance that the loop and update will time out the site. If you are going to do the update method I would suggest altering the query to only get posts where no value exists as it will get less posts and if it times out you can reload the page until it completes.

    
        // 1. get all the posts first
        $allPosts = new WP_Query([
            'post_type' => 'post', // assuming your field is added on 'post' post type
            'posts_per_page' => -1,
            'meta_query' => array(
                'key' => 'your field name here',
                'compare' => NOT EXISTS'
            )
        ]);