Support

Account

Home Forums ACF PRO Force default value on existing posts

Solving

Force default value on existing posts

    • Giray

    • January 4, 2020 at 11:51 pm

    I have posts. I am creating a new field with a default value. It won’t take it unless I create a new post. I need existing posts to accept the default value of the new field. I found other posts about this problem, but my problem is that I am doing this with acfpro + beaver builder.
    Any ideas?
    Thanks

  • I can’t tell you how to do this for beaver builder specifically, I can only tell you how to make the new default value in the field apply, you would need to get someone that knows beaver builder to figure out how to do it there. Unfortunately, I don’t know of any beaver builder experts that frequent this forum.

    The main issue is that since it’s a new field that has never been updated on old posts that the field and the ACF field key reference entries do not exist in the DB. The important part is the ACF field key reference. Without this reference ACF cannot get the field definition and can’t return the default.

    The simplest solution in php is to use the field key to get the value rather than the field name. This allows ACF to return then default value when it is not set.

    
    $value = get_field('field_XXXXXXXXXX');
    

    The second option is to code it into the template

    
    $value = get_field('your-field-name');
    if (!$value) {
      $value = 'some default value';
    }
    

    the only other way would be to physically insert the default value for every existing post. You’d need to create a function that runs one time.

    1. Query all posts where the value needs to be set and loop over them
    2. use get_field() to get the current field value
    3. if no value is returned use update_field() using the FIELD KEY to update the value.

    Depending on how many posts you need to update this action could time out and not be possible to complete. In this case you could modify the query to only return posts where the meta_key “NOT EXISTS” and cause the function to update to run several times until you get them all.

    • Giray

    • January 5, 2020 at 3:05 am

    Hi. Thanks for this. A little above my pay grade but I’ll try… Appreciate the quick response.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.