Support

Account

Home Forums Add-ons Repeater Field Add/update rows of repeater field on user profile with acf from frontend Reply To: Add/update rows of repeater field on user profile with acf from frontend

  • 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 !