Support

Account

Home Forums General Issues Default Value not set until Update Post

Solved

Default Value not set until Update Post

  • I have just added a new custom field as a True/False field with the default value as True but it seems I have to go into each post and Update the post for that to work. Otherwise the default value appears to be False, even though the checkbox is checked.

    Any ideas? My current code is:

    
    <?php if( get_field('show_sidebar') ): ?>
    <div id="sidebar">
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar') ) : ?><?php endif; ?>
    </div>
    <?php endif; ?>
    

    Thanks in advance.

  • That is correct. If the field is added after the post is created the field will not have the default value until the post is updated. ACF does not retroactively set default values.

    True/False fields are a little tricky because ACF will return false for it if it is not set or if it is not checked.

    The best way, in my experience, to deal with this is to use get_post_meta()

    
    <?php 
        $show_sidebar = get_post_meta($post->ID, 'show_sidebar', true);
        if($show_sidebar === '' || $show_sidebar === '1'): ?>
    <div id="sidebar">
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar') ) : ?><?php endif; ?>
    </div>
    <?php endif; ?>
    
  • Thanks for this John. Really appreciate the speedy response.

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

You must be logged in to reply to this topic.