0

How to conditionally display an ACF custom textarea contents only to those users chosen from an ACF User field.

I have an ACF User field (allowed_users)that author's can multiple select when they create a post. I also have an ACF textarea field (private) in the author's post (author's only have 1 post). I'm trying to conditionally display the textarea on the front end only for those users the author selected. This is what I have...

<?php
$allowed_users = get_field('allowed_users');
$custom_field_value = get_field('private');
$current_user_id = get_current_user_id();
if (in_array($current_user_id, $allowed_users)) {
    echo $custom_field_value;
} else {
    echo 'You do not have permission to view this content.';
}
?>

2 Answers 2

1

I'm quite new to this so I might be the wrong person to answer your question but:

get_field('allowed_users')

Returns an array of user IDs so you can't directly use in_array()

So if you change the if statement to:

if (is_array($allowed_users) && in_array($current_user_id, $allowed_users))

Hopefully it will work.

1

You may first check the return value for your ACF User field (allowed_users), in this case the return value should be set for return User ID only, then your code should works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.