Support

Account

Home Forums Bug Reports Repeater fields should never return a string

Solving

Repeater fields should never return a string

  • Repeater fields and any other field that is dealt with like an array (Flexible layout, gallery, etc) should never return a value other than null or an array.

    Right now it seems like an empty repeater returns “0” after saving a post. This makes it so now my code has to check if the value is an array and then convert it to an empty array if not.

  • Hi @emrl

    Which version of ACF are you using? Could you provide me with the details so that I investigate more on this.

  • Hello @acf-support

    I’m using Advanced Custom Fields Pro 5.2.5

    I’ve always noticed this behavior though.

    For example, if you have a repeater field name ‘list’.

    $list = get_field('list');

    should return an array of sub fields. but if a post hasn’t been saved since that custom field has been added (or it has been saved but doesn’t have any rows in the repeater), it will not have any database field for that ‘list’ value, and ACF thus returns a null or 0 value. But it should return an empty array, as that is what you would get if the post had been saved with rows of values.

    You should be able to foreach (get_field('list') as $item), but right now that can result in PHP warnings because get_field('list') isn’t always an array.

    Thanks.

  • Hi @emrl

    Thanks for suggesting this.

    get_field() is a diverse function. For instance, you may want to hide empty fields in your template using conditionals and this could be why get_field() returns null or false if a field does not contain any values.

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

You must be logged in to reply to this topic.