Support

Account

Home Forums General Issues New field not saved in database

Solving

New field not saved in database

  • Hi,

    I added a new field (type: radio button) with a default value to all of my posts. Now I saw that the field with its value isn’t saved to the posts in the database.

    I figure out that I’ve got to click on the “Update”-Button on every post, so the value of the new field is saved to that post in the database.

    Is there a way that every post gets this new value without updating each post?

    I tried this:
    https://jboullion.com/update-acf-post-fields/

    I also used wp_update_post(), but nothing works yet..

  • Hi,

    In your case, you are only updating 1 field, it’d be better to just target that field in the loop. It’s just safer to not messed up other saved value.

    try something like this:

    
    <?php
    
    function temp_update_value() {
        // 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
        ]);
    
        // 2. loop through each posts, and update the acf value
        while ($allPosts->have_posts()): $allPosts->the_post();
            // 3. check if the value is already set, if it does, go to next post
            if (get_field('key_1234567890') !== null) {
                continue;
            }
    
            // 3. update the acf value by the field key
            update_field('key_1234567890', 'your value');
        endwhile; wp_reset_query();
    }
    
    // 4. just in case, use acf/init to make sure acf is finish initilizing
    add_action('acf/init', 'temp_update_value');
    

    put this inside your functions.php file, and refresh any page once to kick it off. After than remember to remove or comment out from you functions.php so it doesn’t get run every time.

    Cheers.

  • 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'
            )
        ]);
    
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘New field not saved in database’ is closed to new replies.