Home › Forums › General Issues › New field not saved in database › Reply To: New field not saved in database
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.