Home › Forums › ACF PRO › Using User Relationship Field to create User Specific Content › Reply To: Using User Relationship Field to create User Specific Content
Your checking needs to be a bit more complicated than this to take account for all situations. This should do the trick for you:
<?php
//Fetch users and set our boolean to true per default
$users = get_field('associated_users');
$show_content = true;
if ( !is_user_logged_in() && $users) {
//If the visitor isn't logged in and we have users they should not be able to see the content
$show_content = false;
}elseif( is_user_logged_in() && $users ){
//Okay so they're logged in and we have users we need to check against
$current_user = wp_get_current_user();
//assume they should not see it (just in case)
$show_content = false;
foreach( $users as $user ){
//Loop through each user array from the ACF field
if( $user['ID'] == $current_user->ID ){
//Alright they are in the clear! Lets set our boolean to true and break early!
$show_content = true;
break;
}
}
}
?>
<?php if( $show_content ){ ?>
<!-- All content goes in here -->
<?php } ?>
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.