Support

Account

Home Forums Backend Issues (wp-admin) add_sub_row() not filling every field

Solving

add_sub_row() not filling every field

  • Hello,

    I think that I have encountered a bug. I create a new post via the function “wp_insert_post()” via the frontend. The post contains a repeater field. I fill this directly after creating with:

    $tabs = array("Tab1", "Tab2", "Tab3");
    
    foreach($tabs AS $tab) {
                $row = array('tab_title' => esc_sql($tab));
                add_row('tabs', $row, $id);
    }

    The newly created tabs again contain repeater fields. If I now want to fill these:

    $row = array(
        'title' => esc_sql($_POST['title']),
        'description' => esc_sql($_POST['description']),
        'file' => $attachment_id,
    );
    add_sub_row(array('tabs', $tab_counter, 'downloads'), $row, $machine_id);

    only the “title” and “file” fields are saved. All other fields are not saved. I have to go to the backend first and manually save the post once to get the corresponding fields saved.

    This could indicate that not all database entries are created correctly in the first step. Or am I doing something wrong?

  • Hi @iamjonasmarlo

    What does $tab_counter contain? It doesn’t seem to be declared anywhere?

    If you change $tab_counter to 1, being the first row of the sub repeater, does it work?

    I assume $machine_id is the post ID?

  • Hey @jarvis

    $tab_counter is set to 1.

    $machine_id ist the post ID.

  • add_row() just like update_field() when a field does not already exist in the DB then you must use field keys instead of field names to add values from PHP

    
    $row = array(
        'fiield_XXXXX' => esc_sql($_POST['title']),
        'fiield_YYYYY' => esc_sql($_POST['description']),
        'fiield_ZZZZZ' => $attachment_id,
    );
    add_sub_row(array('tabs', $tab_counter, 'downloads'), $row, $machine_id);
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.