Home › Forums › Add-ons › Repeater Field › Add/update rows of repeater field on user profile with acf from frontend
Hi everyone,
I’m using ACF 5 (PRO) and I would like to create a watchlist (for each user) linked to each post with a repeater field.
1 – I have created a repeater field named “watchlist_table” with 3 sub fields ( “hub_id”, “hub_status” and “hub_progress”).
2 – Then, I created a front acf form (on each hub page) to let user manage their watchlist with buttons group field (“hub_status” – for example : Completed, Watching, etc.) and number field (“hub_progress” – for each episode).
When a user submit the form, I would like to update his repeater field (add row) with the current post_id (“hub_id”) and others fields (“hub_status, “hub_progress”). And if user submit form twice from the same hub page, update the row with new values.
My problem: I don’t even know if it’s possible. Plus, acf_form + acf/save_post create a post and I only want to add/update row. I’ve searched on acf forums, but I didn’t find any similar topic…
My form:
<?php
// User
$user = wp_get_current_user();
$user_id = 'user_' . $user->ID;
// Form
acf_form(array(
'post_id' => $user_id,
'form' => false, // Add "hub_progress" input myself for dynamic min/max value
'fields' => array('field_5ae014016d4f8'), // Only "hub_status" field
'field_groups => array(1852),
));
?>
How I display form value if already added to watchlist:
<?php
if( have_rows('watchlist_table', $user_id) ):
while( have_rows('watchlist_table', $user_id) ): the_row();
// vars
$hub = get_sub_field('hub_id');
// If sub_field('hub_id') = current hub page where the form is
if ($hub == $post->ID) {
?>
<div><?php echo get_the_title($hub); ?></div>
<div><?php the_sub_field('hub_status'); ?></div>
<div><?php the_sub_field('hub_progress'); ?></div>
<?php }
endwhile;
endif;
?>
Here, I don’t use new_post to create post with acf/save_post
, but I suppose I need to. Then, add to the function with update_row()
and add_row()
and use wp_delete_post()
at the end – maybe ?
If you have any idea, I would appreciate your help ! Thank you !
P.S: Sorry if my english seems weird, I’m French… 🙂 Tell me if I’m not clear !
Hi !
I tried this function:
add_action('acf/save_post', 'save_watchlist');
function save_watchlist( $post_id ){
// Get current user id
$user = wp_get_current_user();
$user_id = 'user_' . $user->ID;
// bail early if editing in admin
if( is_admin() ) {
return;
}
// Get the data
// /!\ Not working here!
$hub_w_id = get_field('hub_w_id', $post_id);
$hub_w_status = get_field('hub_w_status', $post_id);
$hub_w_progress = get_field('hub_w_progress', $post_id);
$row_value = array(
'field_5ae1581e0a445' => $hub_w_id,
'field_5ae1547af0a04' => $hub_w_status,
'field_5ae1547af0a43' => $hub_w_progress,
);
// Set the fake post data
$fake_post = array(
'ID' => $post_id,
'post_type' => 'post',
);
// Remove the hook to avoid infinite loop.
remove_action('acf/save_post', 'save_watchlist');
// Get user watchlist
if( have_rows('watchlist_table', $user_id) ):
while( have_rows('watchlist_table', $user_id) ): the_row();
// vars
$hub = get_sub_field('hub_w_id');
// Add or update ?
if ($hub_w_id == $hub) {
$add = false;
$row = get_row_index();
} else {
$add = '';
}
endwhile;
endif;
// Do it!
if( isset($add) ) {
update_row( 'watchlist_table', $row, $row_value, $user_id );
} else {
add_row( 'watchlist_table', $row_value, $user_id );
}
// Delete fake post
wp_delete_post( $fake_post );
// Add the hook back
add_action('acf/save_post', 'save_watchlist');
}
My form:
acf_form(array(
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'draft'
),
'form' => false, // Add "hub_w_progress" input myself for dynamic min/max value
'fields' => array('field_5ae014016d4f8'), // Only "hub_w_status" field
'field_groups' => array(1852),
));
…and It’s working! My only problem is to get fields from the watchlist form on hub page…
Of course, if you know a cleaner way to do it, tell me ! Thank you !
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.