admin管理员组

文章数量:1325236

I have a Custom Post Type named flashcard and post of that type are listed by an archive-flashcard.php template. The bellow function force flashcard posts to be listed only for posts authors (they have a particular client role) and administrators:

function __set_all_posts_for_author( $query ) {
    if( is_post_type_archive( array( 'flashcard' ) ) &&
        is_user_logged_in() && $query->is_main_query() && !current_user_can( 'manage_options' ) 
    ) {
        $current_user = get_current_user_id();
        $query->set( 'author', $current_user );
    }
}
add_action( 'pre_get_posts', '__set_all_posts_for_author' );

The described CPT have in the permalink a word privat. A second function is redirecting not logged in users, or logged in users that doesn't have a client or an administrator role, if they try to acces a such permalink:

add_action( 'template_redirect', 'wpse_restrict_private' );
function wpse_restrict_private() {
    $user = wp_get_current_user();
    if( strpos( get_permalink(), 'privat' ) !== false ) {
        if( !$user->exists() || //if user is not logged in
            //or it is not a client or an administrator
            !array_intersect( array( 'client', 'administrator' ), $user->roles )
        ) {
            wp_redirect( site_url() ); //redirect to site URL
            exit;
        }
    }
}

The problem is that when logged in users that are not clients or administrators access the flashcard archive (that have the word privat in permalink) they are not redirected and a Not found error is displayed. How can I solve this?

I have a Custom Post Type named flashcard and post of that type are listed by an archive-flashcard.php template. The bellow function force flashcard posts to be listed only for posts authors (they have a particular client role) and administrators:

function __set_all_posts_for_author( $query ) {
    if( is_post_type_archive( array( 'flashcard' ) ) &&
        is_user_logged_in() && $query->is_main_query() && !current_user_can( 'manage_options' ) 
    ) {
        $current_user = get_current_user_id();
        $query->set( 'author', $current_user );
    }
}
add_action( 'pre_get_posts', '__set_all_posts_for_author' );

The described CPT have in the permalink a word privat. A second function is redirecting not logged in users, or logged in users that doesn't have a client or an administrator role, if they try to acces a such permalink:

add_action( 'template_redirect', 'wpse_restrict_private' );
function wpse_restrict_private() {
    $user = wp_get_current_user();
    if( strpos( get_permalink(), 'privat' ) !== false ) {
        if( !$user->exists() || //if user is not logged in
            //or it is not a client or an administrator
            !array_intersect( array( 'client', 'administrator' ), $user->roles )
        ) {
            wp_redirect( site_url() ); //redirect to site URL
            exit;
        }
    }
}

The problem is that when logged in users that are not clients or administrators access the flashcard archive (that have the word privat in permalink) they are not redirected and a Not found error is displayed. How can I solve this?

Share Improve this question edited Aug 22, 2020 at 19:44 Iurie asked Aug 22, 2020 at 19:25 IurieIurie 1,1314 gold badges25 silver badges46 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please check the code where you wrote array_intersect. array_intersect function return null array if it doesn't find any match. So your condition checking using !array_intersect is invalid.

本文标签: wp queryRedirect logged in users from a custom post type archive page if they don39t have a specific role