Support

Account

Home Forums General Issues Checking current logged in user against acf user multiselect field Reply To: Checking current logged in user against acf user multiselect field

  • My first thought is that this would probably work if it weren’t a multiselect. It being a multiselect means that $value will be outputting an array of users. You’ll need to stick a foreach loop in there to confirm, something like:

    <div class="module_col">
    
    		<h3><?php the_sub_field('module_title'); ?></h3>
    
    		<?php
    		// Get List of students to check for eligibility
    		$students_list = get_sub_field('student_availability' );
    
    		// Set the default state to false, to be changed later
    		$student_authorised = false;
    
    		// Loop through students_list to check if user is allowed access
    		foreach( $students_list as $student ) :
    
    			// If user is in the list, set student_authorised to true and break out of loop
    			//(no need to loop through everybody if they're the first user)
    			if( in_array( $student_username, $student['nickname'] ) )
    				$student_authorised = true;
    				break;
    
    		endforeach;
    
    		// If student is in the list, show download link
    		if ( $student_authorised ) : ?>
    		
    
    			<a href="<?php the_sub_field('module_download'); ?>">
    				<button class="download_btn">
    					<span><i class="fa fa-cloud-download"></i>Download Module</span>
    				</button>
    			</a>
    		
    
    		<?php 
    		// if student not in list, show authentication error
    		else : echo 'Not logged in'; endif; ?>
    
    	</div>