Home › Forums › Backend Issues (wp-admin) › 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?
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?
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);
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.