您可以使用过滤器views_edit-post
(或views_edit-{custom-post-type}
) 要修改可用的“视图”,请执行以下操作:
add_filter(\'views_edit-post\', \'cyb_remove_pending_filter\' );
function cyb_remove_pending_filter( $views ) {
if( isset( $views[\'pending\'] ) ) {
unset( $views[\'pending\'] );
}
return $views;
}
在上述过滤器中,您需要包含要应用的用户规则。例如,如果要仅为无法编辑其他帖子的用户删除“挂起”视图,请执行以下操作:
add_filter(\'views_edit-post\', \'cyb_remove_pending_filter\' );
function cyb_remove_pending_filter( $views ) {
if( isset( $views[\'pending\'] ) && ! current_user_can(\'edit_others_posts\') ) {
unset( $views[\'pending\'] );
}
return $views;
}
此外,如果排除挂起的视图,则需要更新“全部”post计数:
add_filter(\'views_edit-post\', \'cyb_remove_pending_filter\' );
function cyb_remove_pending_filter( $views ) {
if( isset( $views[\'pending\'] ) && ! current_user_can(\'edit_others_posts\') ) {
unset( $views[\'pending\'] );
$args = [
// Post params here
// Include only the desired post statues for "All"
\'post_status\' => [ \'publish\', \'draft\', \'future\', \'trash\' ],
\'all_posts\' => 1,
];
$result = new WP_Query($args);
if($result->found_posts > 0) {
$views[\'all\'] = sprintf(
\'<a href="%s">\'.__(\'All\').\' <span class="count">(%d)</span></a>\',
admin_url(\'edit.php?all_posts=1&post_type=\'.get_query_var(\'post_type\')),
$result->found_posts
);
}
}
return $views;
}
此代码仅删除屏幕中的过滤器,但不会阻止对处于挂起状态的帖子的访问。阻止您需要使用的访问
pre_get_posts
行动例如:
add_action( \'pre_get_posts\', \'cyb_exclude_pending_posts\' );
function cyb_exclude_pending_posts( $query ) {
// only in admin, main query and if user can not edit others posts
if( is_admin() && ! current_user_can(\'edit_others_posts\') && $query->is_main_query() ) {
// Include only the desired post statues
$post_status_arg = [ \'publish\', \'draft\', \'future\', \'trash\' ];
$query->set( \'post_status\', $post_status_arg );
}
}