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

    • room34

    • July 22, 2018 at 3:18 am

    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/

    • room34

    • July 25, 2018 at 5:28 am

    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.

    • room34

    • July 25, 2018 at 5:38 am

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

    • room34

    • July 25, 2018 at 6:56 am

    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.

    • adjTeam

    • August 2, 2018 at 7:00 am

    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.

    • adjTeam

    • August 2, 2018 at 10:59 pm

    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.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.