Support

Account

Home Forums General Issues get_field() returning array for Select when no value exists in database

Solved

get_field() returning array for Select when no value exists in database

  • I recently added a new Select field to an existing field group. Since this is a new field, existing pages with that field group do not have wp_postmeta records in the database for this new field.

    My assumption then was that using get_field() on those pages should return a null value… after all, there is no value for that field on the given page!

    However, I found get_field() is actually returning a 1-item array, with key = 0 and value = the first option from the Select dropdown. (I was thinking it was returning that because I have it set as the default for the field, but I tested changing the default, and it had no effect on the output.) Aside from not being the value I expected to have returned, I find it odd that it’s returning an array instead of a string, because I have Return Format set to “Value” for this field.

    Is this intended functionality? If so, what is the purpose? As it is, I have to write a very awkward conditional to account for this output on pages that haven’t been edited since before I added the new field.

  • If you have the field set to only allow a single value then it should not be returning an array. Not sure why it is. Please submit a bug report here https://support.advancedcustomfields.com/new-ticket/

  • The first item in the changelog for version 5.7.1 sounds like a fix for this bug. Do you know if that’s correct? (I’m not asking you to do any work to find out… just if you know for sure or not already.)

  • I don’t know for sure, but it does sound that way to me.

  • Thanks… I may not have a chance to test this immediately but I’ll check it out as soon as I can.

  • The issue appears to be fixed with this update. I’ve removed the workaround code I had added to address it, and my page is still working as expected.

  • I’m still having this problem, even after upgrading to 5.7.1.

    It’s exactly as @room34 describes it: A select set to return “value”. On posts that haven’t yet been saved with the new value, I’m expecting no value at all, but getting a 1-length array with the first item as the value.

    This is tough to work around, because we’re trying to introduce a feature without having to touch existing posts. I’ll post a bug report.

  • Temporary workaround for anyone else having this issue:

    function coerce_array_to_value( $value, $post_id, $field ) {
      if (
        $field['return_format'] === 'value' &&
        !$field['multiple'] &&
        is_array( $value ) &&
        array_key_exists( 0, $value )
      ) {
        $value = $value[0];
      }
    
      return $value;
    }
    
    add_filter( 'acf/load_value/type=select', array( $this, 'coerce_array_to_value' ), 10, 3 );
  • Hi folks,

    Surprisingly, with version 5.8.7, I’m experiencing the same issue with get_sub_field().
    Default value is outputted as an array while a set value outputs as string.

  • Hi people,

    I’m experiencing the same issue with version 5.8.8 after using get_field() on a select field. When it is initially not set (but has a default value set), it returns an array rather than the single pre-selected value. This is breaking the block I’m using it on.

    Any news on a fix?

  • I am having the same issue on 5.8.11 with select fields returning an array when default value set but no value selected.

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

The topic ‘get_field() returning array for Select when no value exists in database’ is closed to new replies.