这超越了帖子和媒体。我有几个CPT和一个日历。有没有办法让wordpress检查用户名并只显示他们创建的内容?
答案 0 :(得分:2)
在后端,要过滤显示的所有帖子类型并限制可视化,您可以使用pre_get_posts
。
add_action( 'pre_get_posts', 'users_own_content_so_12761756' );
/**
* Show only posts of the current user in the dashboard
* affects posts, pages, media and custom post types
*/
function users_own_content_so_12761756( $wp_query_obj )
{
// Restrict hook to the backend
if( !is_admin() )
return;
global $current_user;
get_currentuserinfo();
// http://php.net/manual/en/function.is-a.php
if( !is_a( $current_user, 'WP_User') )
return;
if( !current_user_can( 'administrator' ) )
$wp_query_obj->set( 'author', $current_user->ID );
}
应用此代码后,您会注意到帖子计数不正确:它将显示总计数而不是用户计数。要进行调整,请参阅此问答:Update post counts (published, draft, unattached) in admin interface。
你也需要关心user roles and capabilities,阻止编辑其他人的帖子/页面/ cpts的权利。这是因为用户可以输入浏览器地址example.com/wp-admin/post.php?post=POST_ID&action=edit
并访问帖子,如果他/她有权这样做的话。
答案 1 :(得分:0)
您可以尝试将其添加到循环
<?php $author = get_the_author();
$current_user = wp_get_current_user();
if($author != $current_user->user_nicename) {
echo "permission denied";
break;
} ?>
答案 2 :(得分:0)
我使用成员插件为用户创建自定义角色。