Support

Account

Home Forums General Issues update_field in a loop of a list of users

Solved

update_field in a loop of a list of users

  • I have been working with ACF support trying to get this resolved but we’ve reached the point of being beyond their scope of what they support, so I’m trying the forum to see if any of you helpful and knowledgeable folks can point me in the right direction.

    What I’m trying to do is update a custom user meta field on the front end for a list of users. A single True/False field, for each user in the list. I tried using acf_form but that puts a checkbox and update button next to each user name in my loop. I want the checkbox next to each user with an update button at the bottom of the list. I also need to see the updated status (checked/unchecked) after the form is updated. I have everything working to show the current status of each user as desired, but if I change the status (check, uncheck the checkbox) of a user it is not updated when I submit the form.

    Here’s my code. Appreciate any help!

    	if( current_user_can( 'group_leader' ) || current_user_can( 'administrator' ) ) {
    		$group_leader_id = get_current_user_id();
    		$user_group_ids = learndash_get_administrators_group_ids( $group_leader_id, true );
    		if ( empty( $user_group_ids ) ) {
    			$validation['message'] = __( 'You do not have any groups available to manage.', 'uncanny-learndash-groups' );
    
    			return $validation;
    		}
    
    		$args = array(
    			'numberposts' => 9999,
    			'include'     => array_map( 'intval', $user_group_ids ),
    			'post_type'   => 'groups',
    			//'meta_key'    => SharedFunctions::$code_group_id_meta_key,
    		);
    
    		$ulgm_managed_group_objects = get_posts( $args );
    
    		if ( ! empty( $ulgm_managed_group_objects ) ) {
    
    			$ulgm_current_managed_group_id = $user_group_ids[0];
    
    		}
    
    		$groups_user_object = learndash_get_groups_users( $ulgm_current_managed_group_id );		
    			foreach ( $groups_user_object as $user ) {
    				$uid = $user->ID;
    			}				
    		if( isset($_POST['status']) ) :
    			update_field('field_598c6559e1b0d', $_POST['tri'], 'user_' . $uid);
    			echo '<p class="success">Status Updated<p>';
    		endif;
    		echo '<h2 class="group-table-heading">Tri Annual Status</h2>';
    		echo '<form action="' . get_the_permalink() . '" method="POST" class="user-email-settings">';
    		foreach ( $groups_user_object as $user ) {
    			$f     = get_user_meta( $user->ID, 'first_name', true );
    			$l     = get_user_meta( $user->ID, 'last_name', true );						
    			$field = get_user_meta( $user->ID, 'tri', true );
    			if( $field == '1' ) {
    				$checked = true;
    			}
    			echo '<div class="user"><input type="checkbox" id="' . $user->ID . '" name="' . $user->ID . '" class="pref" value="1"';
    			if($checked) {
    				echo ' checked';
    			}
    			echo ' /> ' . $f . ' ' . $l . '</div>';
    			$checked = false;
    		}
    		echo '<input type="submit" value="Update Tri Annual Status" name="status" id="status" class="ai-button" />';
    		echo '</form>';
    	}
    	?>}
  • G’day Mate,

    At the first glance of you code, I’ve notice 2 things that might or might not be the issue.

    1. On the line where you update the field, you have $_POST['tri']. I don’t see any form field named ‘tir’ in your code.

    2. The nature of checkout input is that, if the checkbox is not check, nothing will be sent upon submission (not even 0). So usually people do this on the true/false checkbox to ensure some value is sent out:

    
    <input type="hidden" name="some-name" value="0" />
    <input type="checkbox" name="some-name" value="1" />
    

    If above fixes doesn’t resolve, I’d suggest to simply your code to the minimal and make sure it works first, before adding all the custom functions and condition. For example, remove the permission check and fetching specific users. Just list all the users (or first 10), and test if the field updating works first. Then, start adding code back.

    Cheers, 🤓

  • Thanks for your suggestions, Gummi, and for taking the time to respond.

    On the line where you update the field, you have $_POST[‘tri’]. I don’t see any form field named ‘tir’ in your code.

    ‘tri’ is the name for ‘field_598c6559e1b0d’. I’ve tried using only field ID wherever it’s referenced, only field name, and every combination of using both in different places without success.

    I tried adding the hidden field as you suggested but that didn’t change anything.

    I’ll try stripping away the conditional items but I really don’t think that has anything to do with it since all of the conditions are working properly. Still, it’s worth a try since nothing else is making this work!

    Any other ideas?

  • Hi,

    What I mean by the $_POST['tri'] is that, your form output is looking something like this:

    
    <form action="..." method="POST" class="user-email-settings">
        <div class="user">
            <input type="checkbox" id="1" name="1" class="pref" value="1" checked /> first last
        </div>
        <div class="user">
            <input type="checkbox" id="2" name="2" class="pref" value="1" /> first last
        </div>
        ...
        <input type="submit" value="Update Tri Annual Status" name="status" id="status" class="ai-button" />
    </form>
    

    None of the input contains the name ‘tri’. Therefor your $_POST will only contains:

    
    $_POST = [
        1 => 1,
        'status' => 'Update Tri Annual Status'
    ];
    

    1) the name of the checkbox input is the index in the $_POST variable.
    2) if the checkbox is not check, the $_POST variable will not include them.

    —————————————————————————
    Based on my understanding of your code, i think what you want is something like the following:

    
    if (has_some_permission()):
        $users = do_something_to_get_those_users();
    
        if ($_POST['status']) {
            foreach ($users as $user) {
                update_field('field_598c6559e1b0d', $_POST['tri'][$user->ID], $user);
            }
    
            echo '<p class="success">Status Updated<p>';
        }
    
        echo '<h2 class="group-table-heading">Tri Annual Status</h2>';
        echo '<form action="' . get_the_permalink() . '" method="POST" class="user-email-settings">';
    
        foreach ($users as $user) {
            $f     = get_user_meta($user->ID, 'first_name', true);
            $l     = get_user_meta($user->ID, 'last_name', true);                     
            $field = get_user_meta($user->ID, 'tri', true); 
            // is this an acf field?
            // $field = get_field('field_598c6559e1b0d', $user);
            // $field = get_field('tri', $user);
            
            echo '<div class="user">';
                echo sprintf('<input type="hidden" name="tri[%d]" value="0" />', $user->ID);
                echo sprintf('<input type="checkbox" name="tri[%d]" value="1" %s />', $user->ID, $field? 'checked' : '');
                echo " {$f} {$l}";
            echo '</div>';
        }
    
        echo '<input type="submit" value="Update Tri Annual Status" name="status" id="status" class="ai-button" />';
        echo '</form>';
    }
    

    Notice the checkbox’s name attribute is what will get put into the $_POST variable.
    The $_POST variable after this form is submitted will be something like:

    
    $_POST = [
        'tri' => [
            1 => 1,
            2 => 0,
            3 => 0,
            4 => 0
        ],
        'status' => 'Update Tri Annual Status'
    ];
    

    Cheers

  • Man, I am VERY grateful for your help, Gummi. I understand what you’re saying for the most part and I tried the code you provided, but unfortunately got the same results (no update to the checkboxes when submitted). Here’s a snippet of what was output:

    array(2) { 
         ["tri"]=> array(33) { 
              [1]=> string(1) "1" 
              [2352]=> string(1) "1" 
              [886]=> string(1) "1" 

    Does that shed any light on this for you? Obviously I’m out of my depth…

  • Disregard my last post. Your code worked, Gummi! I missed one line in my last test. After discovering that I fixed it and tested and everything worked. Thank you SO much again and again! Just made mmy day!

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

The topic ‘update_field in a loop of a list of users’ is closed to new replies.