If Conditional with two roles

Asked by At

I have been trying but I do not achieve results. I wish that only the author of the post and the users with the shop_manager role can see it, if not redirect the page. If they are authors but they didnt write the post, they wont be able to access it.

the code works for everything except shop_manager users can see it.

  function wpso_author_redirection() {
if ( is_single() ) {
    $current_post_details = get_post( get_the_ID(), ARRAY_A );
    $user_id = get_current_user_id();
    if ($user_id != absint( $current_post_details['post_author']) ||$user 
  ['roles']!= 'shop_manager'  )   {
         wp_redirect( 'https://aa.com/contenido-bloqueado');

1 Answers

Ferran On Best Solutions

In the innermost if statement, you are using || . This means OR and it means that if user is not the author or user role is not shop_manager, the redirection is going to execute.

You have to use AND, && in php:

if ($user_id != absint( $current_post_details['post_author']) && $user['roles']!= 'shop_manager'  )   {
    wp_redirect( 'https://aa.com/contenido-bloqueado');

this AND says that if the user is not the author and the user role is not shop_manager then do the redirect. If whatever of the two is false (user is author or user role is shop_manager) the redirect is not going to execute.