Search code examples
phpwordpressadvanced-custom-fields

Trying to get Default Value of ACF Field before post is saved


I am trying to get the Default Value of an ACF field but to no avail. I've got this code at the moment:

    <section id="location-info">
        <?php if( get_field('location_info') && get_field('is_airport') == false && get_field('is_railway_station') == false): ?>
            <h3>Location info</h3>
            <p><?php the_field('location_info') ?></p>
        <?php elseif ( get_field('airport_info') && get_field('is_airport') == true && get_field('is_railway_station') == false): ?>
            <h3>Airport info</h3>
            <p><?php the_field('airport_info') ?></p>
        <?php elseif ( get_field('railway_info') && get_field('is_airport') == false && get_field('is_railway_station') == true): ?>
            <h3>Railway info</h3>
            <p><?php the_field('railway_info') ?></p>
        <?php elseif ( get_field('is_airport') == true && get_field('is_railway_station') == true): ?>
            <h3>Airport info</h3>
            <p><?php the_field('airport_info') ?></p>
        <?php else: ?>
            <?php if( get_field('is_airport') == false && get_field('is_railway_station') == false): ?>
                <?php echo '1' ?>
                <?php get_field_object('location_info') ?>
                <?php the_field('location_info') ?>
            <?php elseif ( get_field('is_airport') == true && get_field('is_railway_station') == false): ?>
                <?php echo '2' ?>
                <?php get_field_object('airport_info') ?>
            <?php elseif ( get_field('is_airport') == false && get_field('is_railway_station') == true): ?>
                <?php echo '3' ?>
                <?php get_field_object('railway_info') ?>
            <?php else: ?>
                <?php echo 'No info for this location' ?>
            <?php endif; ?>
        <?php endif; ?>
    </section>

See get_field_object('location_info'). This line is meant to grab the Default Value of the field 'Location Info' (or at least I think so) as defined in Field Groups. However, this line is returning false and will continue to return false until I open and save the post.

The big question: is there a way to get the Default Value without needing to save the post first?


Solution

  • You can use the acf_get_field() function to get all of the field data. This returns an array containing default_value.

    acf_get_field('location_info')['default_value']