“全部”视图显示所有帖子,包括中的草稿wp-admin/edit.php
. 如何在all视图中排除具有草稿状态的帖子?
在edit.php的All()视图中排除草稿
2 个回复
SO网友:birgire
这个show_in_admin_all_list
中的参数register_post_status()
函数确定给定的post状态是否包含在post表格视图中。
最短的版本可能是:
add_action( \'init\', function() use ( &$wp_post_statuses )
{
$wp_post_statuses[\'draft\']->show_in_admin_all_list = false;
}, 1 );
但是,让我们避免像这样直接修改全局变量,并覆盖默认值draft
状态为:add_action( \'init\', function()
{
register_post_status( \'draft\',
[
\'label\' => _x( \'Draft\', \'post status\' ),
\'protected\' => true,
\'_builtin\' => true,
\'label_count\' => _n_noop( \'Draft <span class="count">(%s)</span>\', \'Drafts <span class="count">(%s)</span>\' ),
\'show_in_admin_all_list\' => false, // <-- we override this setting
]
);
}, 1 );
其中,我们使用优先级1,因为默认草稿状态注册为优先级0。为了避免重复默认设置并支持将来可能的设置更改,我们可以使用get_post_status_object()
而是:
add_action( \'init\', function()
{
$a = get_object_vars( get_post_status_object( \'draft\' ) );
$a[\'show_in_admin_all_list\'] = false; // <-- we override this setting
register_post_status( \'draft\', $a );
}, 1 );
SO网友:Dave Romsey
下面的代码将删除post
岗位类型。
查询参数all_posts
值为1
添加到菜单链接,以确保我们仅在必要时应用此修改(admin post filters(All,Mine,Published,Sticky,Scheduled,Drafts)下的所有链接将为我们添加此查询参数,但单击管理菜单时并非如此,因此我们需要自己添加它。
将下面的代码放入主题的functions.php
或者在插件中。
// Add a query argument to the Posts admin menu.
// This is used to ensure that we only apply our special filtering when needed.
add_action( \'admin_menu\', \'wpse255311_admin_menu\', PHP_INT_MAX );
function wpse255311_admin_menu() {
global $menu, $submenu;
$parent = \'edit.php\';
foreach( $submenu[ $parent ] as $key => $value ){
if ( $value[\'2\'] === \'edit.php\' ) {
$submenu[ $parent ][ $key ][\'2\'] = \'edit.php?all_posts=1\';
break;
}
}
}
// Hide draft posts from All listing in admin.
add_filter( \'pre_get_posts\', \'wpse255311_pre_get_posts\' );
function wpse255311_pre_get_posts( $wp_query ) {
$screen = get_current_screen();
// Ensure the the all_posts argument is set and == to 1
if ( ! isset( $_GET[\'all_posts\'] ) || $_GET[\'all_posts\'] != 1 ) {
return;
}
// Bail if we\'re not on the edit-post screen.
if ( \'edit-post\' !== $screen->id ) {
return;
}
// Bail if we\'re not in the admin area.
if ( ! is_admin() ) {
return;
}
// Ensure we\'re dealing with the main query and the \'post\' post type
// Only include certain post statuses.
if ( $wp_query->is_main_query() && $wp_query->query[\'post_type\'] === \'post\' ) {
$wp_query->query_vars[\'post_status\'] = array (
\'publish\',
\'private\',
\'future\'
);
}
}