Support

Account

Home Forums Bug Reports get_field() can return either false or null when unset

Helping

get_field() can return either false or null when unset

  • I’m using an image field here (I don’t recall which field types it happens with – perhaps all of them), and when the field has never been set before, its value as returned by get_field() is null. But when the field was set in the past, but now it’s unset, then the value is false.

    ACF should return the same value in both situations.

  • There are times when you want to know if a value has never been set before. For example, if you want to use a default value if it has never been set, but if someone has edited the post and removed the default value then show nothing at all.

    
    $value = get_field('some_field');
    if ($value === NULL) {
      // show default value
    } elseif ($value) {
      // show value from field
    } else {
      // show nothing
    }
    

    There are probably many conditions where it is important to know if a value has never been set or the field simply does not have a value.

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

The topic ‘get_field() can return either false or null when unset’ is closed to new replies.