Home Forums General Issues get_field_object return the group's defined default value?


get_field_object return the group's defined default value?

  • I’m trying to provide an if/else statement in which if the page/post doesn’t define a value for a particular acf field, that field’s default value that is defined in the field options group is shown (which is what the get_field_object docs seem to describe) however I’m finding that its not working as desired.

    $hl_logo = 'headliner_logo_alt_tag';
    $hl_logo_default = get_field_object('headliner_logo_alt_tag');
    if( get_field($hl_logo)) : the_field($hl_logo); else : echo $hl_logo_default['default_value']; endif;

    If a post/page exists prior to the creation of the options group, the default value is never provided because the get_field_object queries the post_meta for the field key which doesn’t exist yet, not that actual field group’s defined field option value saved from within the field group editor (which in turn is its own custom post type).

    Basically I’m trying to get away with using the field option group’s default values as the global value site wide in my templates and the individual page/post can override that but on an existing site with hundreds of posts I have to go update all existing pages/posts for the default value to be provided. For newly created pages and posts, its not an issue because the default value is retrieved via the post meta field key generated automatically.

    My only way around this is a more complex query into the field group’s cusotm post type ‘acf’ and then capture the post_meta that stores the default value.

    If I’m missing something fundamental please help point me in the right direction, thank you.

  • Hmm, looks like I overlooked the $field_key notes further down the docs page which explains why using $field_name is not the best choice.

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

You must be logged in to reply to this topic.