Support

Account

Home Forums Add-ons Repeater Field Repeater field returning row count instead of array

Solving

Repeater field returning row count instead of array

  • Hi

    So I’m working with a repeater field, and API response data, and I’m trying to programatically create repeater rows from my API response data and I’m having some weird issues. I’ve tried many different approaches and it kind of works, but i can’t display my data on the frontend correctly.

    This is my function to set the repeater data:

    private function updateTicketsField($post_id, $item) : void
        {
            $tickets = [];
            $i = 0;
            if (isset($item['rate']['event'][0])) {
                foreach ($item['rate']['event'][0] as $type => $details) {
                    if (isset($details['amount'])) {
                        $tickets[$i]['type'] = ucfirst($type);
                        $tickets[$i]['price'] = floatval($details['amount']);
                        $i++;
                    }
                }
            }
            update_field('tickets', $tickets, $post_id);
        }

    This function is being fired on the save_post action:

    add_action('save_post', [$this, 'saveFields'], 10, 3);

    saveFields is just a parent function, but this calls updateTicketsField(), $item is the API response data as an array.

    So what’s weird about it, is that the Data is actually being saved against the post / in the database correctly. When i go to the Admin for the post i can see the Repeater rows have been added correctly.

    What i have discovered is that for some reason in the database the Row count is being saved under the key:

    “meta_id” “post_id” “meta_key” “meta_value”
    “14193” “105” “tickets” “2”
    “14194” “105” “_tickets” “field_676293d3f9427”

    But because of this whenever i try to do:

    $tickets = get_field('tickets', $id);

    It returns as the row count “2”, instead of the array of data / sub fields.

    Or if i instead do:
    if (have_rows('tickets', $id)) this seems to return as a false value.

    So things I’ve tried:

    Instead of using update_field() with the field names I’ve tried using field keys:

    $tickets[$i]['field_67c82f43a270c'] = ucfirst($type);
    $tickets[$i]['field_67c82f56a270d'] = floatval($details['amount']);

    update_field('field_676293d3f9427', $tickets, $post_id);

    But the same problem persists.

    I’ve also tried using add_row() instead of update_field:

    add_row('tickets', [
    
                         'type' => ucfirst($type),
    
                        'price' => floatval($details['amount']),
    
    ], $post_id);

    But again this has the same issue.

    Between each test I’ve been manually deleting the data directly from the database. and I’ve also used:

    wp_cache_flush();
    delete_post_meta($id, 'tickets');
    delete_post_meta($id, '_tickets');

    to cleanup any rogue values which might be hanging around.

    Oddly enough I’ve tried commenting out the ‘save_post’ filter entirely, and just adding my repeater row data in manually, but it still seems to have the same issue. Even if i delete the Fields and recreate them within the Field Group.

    I’m a bit stuck with this so any help would be appreciated.

  • Just following on from this, I’ve actually created a separate repeater field on a page for testing. And I’ve saved this with a couple rows of test data and it does also seem to save the row count anyway. So maybe that’s not the problem.

    But that leaves me abit stumped, So I’m not really sure why i can’t get my repeater displayed. Maybe a frontend issue with it. But backend side, the data seems to be saving correctly. I just can’t get the actual data displayed on the frontend.

  • I’ve managed to fix this issue. So my website is a multisite & has a Parent / Child Theme setup.

    Apparently the only problem i had was that the field group hadn’t been “imported” in the Child Theme. E.G. ACF > Sync Available > Field Group > Import

    So even though the fields were actually visible in the admin, it wouldn’t return the array of data to me until it was imported.

    I’m not too sure why that’s a requirement but it’s fixed it regardless.

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

You must be logged in to reply to this topic.