Support

Account

Home Forums ACF PRO How to update the db with a default value

Solved

How to update the db with a default value

  • I manage a site that has 350+ posts. This isn’t a huge number, but its not possible to manually update each one.

    I need to create a new field for these posts and I can easily setup a default value for the field.

    How can I update all the post_meta db table with the default value without manually opening and saving all the posts?

  • In most cases it would be much easier to alter the code in your template to check for a value an if it does not exist then show the default.

    
    $value = get_field('my_field');
    if ($value === false) {
        $value = 'default_value';
    }
    

    To do it the other way, you can:

    • create a temporary function that runs on a WP hook
    • Do a query that gets all the posts in the post type you want to update
    • Loop through all the posts and check the field
    • If the field is empty, update the field with the default value

    I generally do the former and code my templates to deal with default values because I assume that any field might be empty.

  • Josh,

    Thanks for the notes. this helps.

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

The topic ‘How to update the db with a default value’ is closed to new replies.