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?
1 Answer
Reset to default 0Please 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 query - Redirect logged in users from a custom post type archive page if they don't have a specific role 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742166822a2425979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论