register_post_status无法注册新文章状态 自定义状态未在文章列表显示
问题核心原因
你当前只完成了自定义状态的基础注册,缺少两个必要适配:最新版WordPress默认使用的区块编辑器需要REST API支持才能识别自定义状态,同时后台编辑页、快速编辑的状态下拉默认不会自动加载自定义状态,你无法给文章设置对应状态,自然不会在列表页显示对应内容。
完整修复代码
1. 修改自定义状态注册逻辑,新增REST支持
// 注册自定义文章状态 function wpb_custom_post_status(){ register_post_status('rejected', array( 'label' => _x( 'Rejected', 'post' ), 'public' => true, 'exclude_from_search' => false, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, 'show_in_rest' => true, // 适配区块编辑器与REST API,必加 'label_count' => _n_noop( 'Rejected <span class="count">(%s)</span>', 'Rejected <span class="count">(%s)</span>' ), )); } add_action( 'init', 'wpb_custom_post_status' );
2. 把自定义状态添加到后台状态选择下拉
// 后台编辑页、快速编辑栏注入自定义状态选项 function wpb_add_custom_status_to_select() { global $post; // 仅适配文章类型,如需适配自定义文章类型可修改判断条件 if ( $post->post_type !== 'post' ) return; ?> <script> jQuery(document).ready(function($){ // 文章编辑页状态下拉 $('select#post_status').append('<option value="rejected">Rejected</option>'); // 自动选中当前文章的状态 <?php if ( $post->post_status === 'rejected' ) : ?> $('select#post_status').val('rejected'); <?php endif; ?> // 快速编辑栏状态下拉 $('.inline-edit-status select').append('<option value="rejected">Rejected</option>'); }); </script> <?php } add_action('admin_footer-post.php', 'wpb_add_custom_status_to_select'); add_action('admin_footer-post-new.php', 'wpb_add_custom_status_to_select'); add_action('admin_footer-edit.php', 'wpb_add_custom_status_to_select');
可选:添加文章列表状态筛选项
如果需要在文章列表顶部单独筛选Rejected状态的文章,可以添加以下代码:
// 文章列表顶部新增自定义状态筛选 function wpb_add_custom_status_filter($post_type) { if ( $post_type !== 'post' ) return; global $wp_query; $current_status = $wp_query->query['post_status'] ?? ''; ?> <select name="post_status" id="post_status_filter"> <option value="">所有状态</option> <option value="publish" <?php selected($current_status, 'publish'); ?>>已发布</option> <option value="draft" <?php selected($current_status, 'draft'); ?>>草稿</option> <option value="pending" <?php selected($current_status, 'pending'); ?>>待审核</option> <option value="rejected" <?php selected($current_status, 'rejected'); ?>>Rejected</option> </select> <?php } add_action('restrict_manage_posts', 'wpb_add_custom_status_filter');
验证方法
添加完代码后,编辑任意一篇文章,将状态修改为Rejected后保存,返回文章「全部」列表页即可看到对应内容。如果仍不显示,检查是否存在自定义pre_get_posts钩子修改了文章查询的post_status参数限制。
内容的提问来源于stack exchange,提问作者Dazeh
相关产品推荐
相关产品推荐

