Hi there, I’m attempting to use your wonderful plugin like so.
I have a custom post type: Client
Within the custom post type I have a ACF for User Relationship
The idea is while creating a client adding a user to the ACF will give those specified users access to view that page, I’m trying the code below to no avail, any insight would be greatly appreciated.
<?php if ( is_user_logged_in() ) { $userID = (int)get_field('associated_users'); $theuser = get_userdata($userID); if ($theuser->user_login == 0 ) { ?>
Hi @sharksharkco
Are you certain the value you get back from the user field is their ID?
I think it’s the wp_user object. https://codex.wordpress.org/Class_Reference/WP_User
Thanks Jonathan, now I’m using the following. It allows logged in users to see but still every logged in user rather than the specified in associated_users. Any ideas?
<?php if ( is_user_logged_in() ) { $userID = (int)get_field('associated_users'); $current_user = wp_get_current_user(); if ($current_user->$userID == 0 ) { ?>
Hi @sharksharkco
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 } ?>
Awesome, that worked great!
Thank you!