我正在设置一个网站,其中将有多个用户作为作者,所有者不希望作者能够查看彼此的帖子,因为有一些元字段,其中包含他宁愿在作者之间共享的信息。

有没有办法删除查看其他作者帖子的能力?

谢谢,查克

为了澄清更多内容,这是针对管理方面的,在帖子下面的顶部,有我的,所有和出版的链接。我只希望作者看到“我的”。

有帮助吗?

解决方案

如果您想阻止用“作者”角色的用户在概述屏幕中查看其他用户的帖子(他们将无法查看详细信息),则可以在作者上添加额外的过滤器:

add_action( 'load-edit.php', 'wpse14230_load_edit' );
function wpse14230_load_edit()
{
    add_action( 'request', 'wpse14230_request' );
}

function wpse14230_request( $query_vars )
{
    if ( ! current_user_can( $GLOBALS['post_type_object']->cap->edit_others_posts ) ) {
        $query_vars['author'] = get_current_user_id();
    }
    return $query_vars;
}

邮表上方的小链接(“ mine”,“ all”,“草稿”)现在不太有用,您也可以删除它们:

add_filter( 'views_edit-post', 'wpse14230_views_edit_post' );
function wpse14230_views_edit_post( $views )
{
    return array();
}

其他提示

这正是默认“作者”角色所做的。http://codex.wordpress.org/roles_and_capabilities

只需检查功能(请参阅@WYCK的链接)和模板内的作者ID,然后将您不希望其他人看到的内容放置在IF/else中:

// Get the author of this post:
$post_author = get_query_var('author_name') ? get_user_by( 'slug', get_query_var('author_name') ) : get_userdata( get_query_var('author') );

// Get data from current user:
global $current_user;
get_currentuserinfo();
// Get the display_name from current user - maybe you have to exchange it with $current_user->user_login
$current_author = $current_user->display_name;

// Check the capability and if the currently logged in user is the the post author
if ( current_user_can('some_capability') && $post_author == $current_author )
{
    // Post Meta
    $post_meta = get_post_meta( $GLOBALS['post']->ID );
    // DO OR DISPLAY STUFF HERE
}

我今天必须做这样的事情,这就是为什么我找到了这篇文章的原因。我发现对我有用的是这篇标题为:“如何将作者限制在WordPress Admin中“由WPBEGINNER

这是您可以粘贴到functions.php的代码:

function posts_for_current_author($query) {
    global $pagenow;

    if( 'edit.php' != $pagenow || !$query->is_admin )
        return $query;

    if( !current_user_can( 'edit_others_posts' ) ) {
        global $user_ID;
        $query->set('author', $user_ID );
    }
    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

在这里查看一个更完整的解决方案(还可以修复邮政计数的过滤器栏): 帮助凝结/优化一些工作代码

许可以下: CC-BY-SA归因
scroll top