我想知道这是否可能。我创建了一个客户端门户,在那里他们可以看到分配给他们的所有工作(这些都显示在前端)。一旦他们登录,他们就会被重定向到他们的页面。我正在使用下面的代码显示与当前用户相关的项目
<?php if ( is_user_logged_in() ) : function filter_posts_by_author( $query ) {
global $current_user; get_currentuserinfo();
$query->set( \'author\', $current_user->ID );
}
add_action( \'pre_get_posts\', \'filter_posts_by_author\' );?>
<h2>Post Goes here</h2>
<?php else: wp_die(\'Sorry, you do not have access to this page. Please <a href="/#/">sign in</a> to view this page.\');endif; ?>
这很好用。我试图解决的问题是,当您以管理员身份登录时,如何显示所有用户的所有帖子(在前端)
我试过了,但它会向所有客户端和管理员显示所有帖子
<?php if ( is_user_logged_in() ) : function filter_posts_by_author( $query ) {
global $current_user; get_currentuserinfo();
$query->set( \'author, administrator\', $current_user->ID );
}
add_action( \'pre_get_posts\', \'filter_posts_by_author\' );?>
当管理员登录时,如何显示所有帖子,而当用户登录时,如何仅显示所选帖子?
最合适的回答,由SO网友:Howdy_McGee 整理而成
您可以尝试这样做,如果当前用户已登录而不是管理员,它将从当前用户中提取所有帖子,否则它将默认显示并显示所有用户的所有帖子。
<?php
function filter_posts_by_author( $query ) {
if( is_user_logged_in() ) {
if( !current_user_can( \'administrator\' ) ) {
global $current_user;
get_currentuserinfo();
$query->set( \'author\', $current_user->ID );
}
}
else {
wp_die(\'Sorry, you do not have access to this page. Please <a href="/#/">sign in</a> to view this page.\');
}
}
add_action( \'pre_get_posts\', \'filter_posts_by_author\' );
?>