Support

Account

Home Forums Add-ons Repeater Field Return repeater values by User value

Solved

Return repeater values by User value

  • Hello. I have a repeater field that contains a “User” subfield. When displaying the repeater data, I’d like to only show the user the values that match the repeater row where the “User” field was set to their user. I’m currently faking this by using the user ID to set a class and using CSS to show/hide based on the current user ID but I have some additional functionality that really requires me to do this the right way, namely, only show the repeater row values if the “user” value matches the current user.

    Any help is appreciated, thanks!

  • Hi @fontsnob,

    You can check if the current user is the selected user like this:

    // check if the repeater field has rows of data
    if( have_rows('repeater_field_name') ):
    
     	// loop through the rows of data
        while ( have_rows('repeater_field_name') ) : the_row();
            
            $currentUserID = get_current_user_id();
            $allowedUser = get_sub_field('user_sub_field_name');
            
            if($currentUserID == $allowedUser['ID']){
                the_sub_field('other_sub_field_name');
            }
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;

    Please change the field name according to your field name. You can also debug the user field value by using var_dump(). This page should give you more idea about it: http://www.advancedcustomfields.com/resources/debug/.

    I hope this helps.

  • BOOM, that’s perfect. Very powerful stuff here. Thanks!

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

The topic ‘Return repeater values by User value’ is closed to new replies.