隐藏特定管理员用户的帖子

时间:2016-06-11 作者:bakingsoda

我目前正试图修改我的WordPress网站,以便作者在编辑时无法看到特定管理员的帖子,但仍然能够看到彼此的帖子(以及其他管理员)。

使用下面的代码,我可以使作者只能查看自己的帖子,并且只有他们的帖子才会计入帖子数量。

如何修改它,使其仅隐藏特定管理员的帖子和帖子数?

add_action(\'pre_get_posts\', \'query_set_only_author\' );
function query_set_only_author( $wp_query ) {
global $current_user;
  if( is_admin() && !current_user_can(\'edit_others_posts\') ) {
      $wp_query->set( \'author\', $current_user->ID );
      add_filter(\'views_edit-post\', \'fix_post_counts\');
  }
}

// Fix post counts
function fix_post_counts($views) {
    global $current_user, $wp_query;
    unset($views[\'mine\']);
    $types = array(
        array( \'status\' =>  NULL ),
        array( \'status\' => \'publish\' ),
        array( \'status\' => \'draft\' ),
        array( \'status\' => \'pending\' ),
        array( \'status\' => \'trash\' )
    );
    foreach( $types as $type ) {
        $query = array(
            \'author\'      => $current_user->ID,
            \'post_type\'   => \'post\',
            \'post_status\' => $type[\'status\']
        );
        $result = new WP_Query($query);
        if( $type[\'status\'] == NULL ):
            $class = ($wp_query->query_vars[\'post_status\'] == NULL) ? \' class="current"\' : \'\';
            $views[\'all\'] = sprintf(__(\'<a href="%s"\'. $class .\'>All <span class="count">(%d)</span></a>\', \'all\'),
                admin_url(\'edit.php?post_type=post\'),
                $result->found_posts);
        elseif( $type[\'status\'] == \'publish\' ):
            $class = ($wp_query->query_vars[\'post_status\'] == \'publish\') ? \' class="current"\' : \'\';
            $views[\'publish\'] = sprintf(__(\'<a href="%s"\'. $class .\'>Published <span class="count">(%d)</span></a>\', \'publish\'),
                admin_url(\'edit.php?post_status=publish&post_type=post\'),
                $result->found_posts);
        elseif( $type[\'status\'] == \'draft\' ):
            $class = ($wp_query->query_vars[\'post_status\'] == \'draft\') ? \' class="current"\' : \'\';
            $views[\'draft\'] = sprintf(__(\'<a href="%s"\'. $class .\'>Draft\'. ((sizeof($result->posts) > 1) ? "s" : "") .\' <span class="count">(%d)</span></a>\', \'draft\'),
                admin_url(\'edit.php?post_status=draft&post_type=post\'),
                $result->found_posts);
        elseif( $type[\'status\'] == \'pending\' ):
            $class = ($wp_query->query_vars[\'post_status\'] == \'pending\') ? \' class="current"\' : \'\';
            $views[\'pending\'] = sprintf(__(\'<a href="%s"\'. $class .\'>Pending <span class="count">(%d)</span></a>\', \'pending\'),
                admin_url(\'edit.php?post_status=pending&post_type=post\'),
                $result->found_posts);
        elseif( $type[\'status\'] == \'trash\' ):
            $class = ($wp_query->query_vars[\'post_status\'] == \'trash\') ? \' class="current"\' : \'\';
            $views[\'trash\'] = sprintf(__(\'<a href="%s"\'. $class .\'>Trash <span class="count">(%d)</span></a>\', \'trash\'),
                admin_url(\'edit.php?post_status=trash&post_type=post\'),
                $result->found_posts);
        endif;
    }
    return $views;
}

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

试试这个-与其重做WordPress为查看链接所做的所有艰苦工作,不如计算您想要“隐藏”的管理员的所有帖子,并从现有帖子数量中减去:

function wpse_229427_get_hidden_admin_id() {
    return 3; // You could make this a setting or return a value conditionally
}

function wpse_229427_hide_admin_posts( $wp_query ) {
    if ( is_admin() && ! current_user_can( \'edit_others_posts\' ) && ! $wp_query->get( \'author\' ) ) {
        $wp_query->set( \'author__not_in\', [ wpse_229427_get_hidden_admin_id() ] );
    }
}

add_action( \'pre_get_posts\', \'wpse_229427_hide_admin_posts\' );

function wpse_229427_adjust_counts( $counts, $type ) {
    if ( $type === \'post\' && is_admin() && ! current_user_can( \'edit_others_posts\' ) ) {
        global $wpdb;

        $admin_id = wpse_229427_get_hidden_admin_id();
        $items    = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT post_status, COUNT( * ) AS `count` FROM {$wpdb->posts} WHERE post_type = \'post\' AND post_author = %d GROUP BY post_status",
                $admin_id
            )
        );

        foreach ( $items as $item ) {
            if ( isset( $counts->{$item->post_status} ) )
                $counts->{$item->post_status} -= $item->count;
        }
    }

    return $counts;
}

add_filter( \'wp_count_posts\', \'wpse_229427_adjust_counts\', 10, 2 );

相关推荐

当作者ID和用户ID匹配时,is_Author(Get_Current_User_id())返回FALSE

在循环内部,作为登录到localhost测试站点时$user->ID == 1 功能is_author(get_current_user_id()) 退货false 什么时候get_the_author_meta(\'ID\') 返回1。因此,永远不会执行以下条件(is_user_logged_in() 退货true):if( is_user_logged_in() && is_author(get_current_user_id()) ) { // do stuff