Home Forums Add-ons Flexible Content Field Flexible Content – get_fields – is the order guaranteed?


Flexible Content – get_fields – is the order guaranteed?

  • Flexible content is great…but it’s not great for allowing a user to build forms. When using get_fields or get_field_objects, there is no unique identifier for each specific field. There is only the key (which would be the same for multiple fields).

    So trying to build a form with key=>value pairings, is almost impossible. I came up with a version that uses get_sub_field_object when recursing with php, but now I’m doing a client side version that sends the flexible content data to the client to format the form. This is proving problematic.

    In short, if I do, get_fields, is the order guaranteed to the same if I run that function again later on (assuming the form has not changed). If so, then I can just use the Array indexes.

    Otherwise, is there a best practices for this?

    Hopefully I’m making sense.

  • get_field() will, as far as I know, return the fields in the order that the meta values were created.

    The “unique” part is difficult when using repeaters and flexible content, which is the reason that ACF uses the method that it does for storing the data in the database. Flexible content and repeaters can be considered the same when it comes to how they are stored. The only difference is that the meta value for a repeater is then number of rows and the flex field is an array of layout. But, perhaps the way that ACF works will help you. The “meta_key” values for repeater and flex field layouts are

    // $repeater  = name of repeater or flex field
    // $index     = row, starting at 0
    // $sub_field = name of sub_field

    nested repeaters look something like this


    At any rate, this is how ACF creates unique meta keys

    Not sure if any of this helps you at all

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

You must be logged in to reply to this topic.