WordPress自定义分类无法编辑或删除问题求助
解决WordPress自定义分类无法编辑/删除的问题
我来帮你搞定这个头疼的问题!从你贴的代码和截图来看,虽然你已经尝试配置了capabilities,但大概率是权限映射的细节没处理到位,或者缓存在搞鬼。咱们一步步来排查修复:
核心问题分析
你给自定义分类法设置了manage_categories这类默认权限,但自定义文章类型(anwaelte)的权限配置比较简略,可能导致WordPress无法正确识别管理员对该分类的操作权限;另外也有可能是权限缓存或者注册逻辑的小问题。
修复方案
方案1:完善自定义文章类型与分类法的权限映射
把自定义文章类型的权限配置明确化,同时让分类法的权限和它对齐,这样WordPress能更清晰地识别权限关系:
修改你的bm_custom_post_type函数,更新自定义文章类型的配置,并且调整分类法的权限:
function bm_custom_post_type() { $labels = array( 'name' => __( 'Anwälte' ), 'singular_name' => __( 'Anwalt' ), 'add_new' => __( 'Anwalt hinzufügen' ), 'add_new_item' => __( 'Anwalt hinzufügen' ), 'edit_item' => __( 'Anwalt bearbeiten' ), 'new_item' => __( 'Anwalt hinzufügen' ), 'view_item' => __( 'Anwalt ansehen' ), 'search_items' => __( 'Anwalt durchsuchen' ), 'not_found' => __( 'Keinen Anwalt gefunden...' ), 'not_found_in_trash' => __( 'Keinen Anwalt im Papierkorb gefunden.' ), 'parent_item_colon' => '' ); $fields = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'capability_type' => 'anwalt', // 改成自定义的权限类型,替代默认的post 'map_meta_cap' => true, // 开启元权限映射,这很重要! 'menu_icon' => 'dashicons-businessman', 'hierarchical' => false, 'menu_position' => null, 'rewrite' => array( 'slug' => 'anwaelte' ), 'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes', 'revisions' ), // 明确自定义权限 'capabilities' => array( 'edit_post' => 'edit_anwalt', 'edit_posts' => 'edit_anwalte', 'edit_others_posts' => 'edit_others_anwalte', 'publish_posts' => 'publish_anwalte', 'read_post' => 'read_anwalt', 'read_private_posts' => 'read_private_anwalte', 'delete_post' => 'delete_anwalt' ) ); register_post_type('anwaelte', $fields); // 更新第一个分类法的权限 register_taxonomy( 'expertise', array( 'anwaelte', 'page' ), array( 'capabilities' => array( 'manage_terms' => 'manage_anwalte_categories', 'edit_terms' => 'manage_anwalte_categories', 'delete_terms' => 'manage_anwalte_categories', 'assign_terms' => 'edit_anwalte' ), 'label' => __( 'Expertise' ), 'hierarchical' => true, 'rewrite' => array('slug' => 'tax-expertise'), 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, ) ); // 更新第二个分类法的权限 register_taxonomy( 'rechtsgebiete', array( 'anwaelte' ), array( 'capabilities' => array( 'manage_terms' => 'manage_anwalte_categories', 'edit_terms' => 'manage_anwalte_categories', 'delete_terms' => 'manage_anwalte_categories', 'assign_terms' => 'edit_anwalte' ), 'hierarchical' => true, 'label' => __( 'Rechtsgebiete' ), 'rewrite' => array('slug' => 'tax-rechtsgebiete'), 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, ) ); } add_action('init', 'bm_custom_post_type'); // 给管理员角色添加自定义权限 function bm_add_custom_capabilities() { $admin_role = get_role('administrator'); if ($admin_role) { $admin_role->add_cap('manage_anwalte_categories'); $admin_role->add_cap('edit_anwalte'); $admin_role->add_cap('edit_others_anwalte'); $admin_role->add_cap('publish_anwalte'); $admin_role->add_cap('read_private_anwalte'); $admin_role->add_cap('delete_anwalt'); } } add_action('admin_init', 'bm_add_custom_capabilities');
方案2:刷新WordPress权限缓存
有时候WordPress会缓存角色权限,哪怕你改了代码也不生效。可以试试:
- 切换到其他用户角色再切回管理员
- 进入
wp_options数据库表,删除user_roles选项(操作前记得备份数据库!),然后重新登录后台
方案3:排除主题/插件冲突
暂时切换到WordPress默认主题(比如Twenty Twenty-Four),或者禁用所有第三方插件,看看问题是否消失。如果消失了,再逐个排查是哪个主题/插件导致的冲突。
关键说明
map_meta_cap => true这个配置一定要加,它能让WordPress正确处理复杂的权限逻辑- 自定义权限类型比用默认的
post更不容易出现冲突 - 添加完权限后,务必刷新权限缓存,否则新权限不会生效
内容的提问来源于stack exchange,提问作者Sascha Rudolph
相关产品推荐
相关产品推荐

