You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress CPT行操作消失,如何仅保留View与Trash?

解决自定义文章类型行操作消失并保留View/Trash的问题

我来帮你搞定这个问题!你遇到的行操作消失,核心原因是capabilities配置不完整或者权限没有正确分配,导致WordPress判断当前用户没有操作权限,所以隐藏了所有行操作。下面分两步解决:

第一步:正确配置CPT的权限设置

首先,在register_post_type()里,你需要确保权限配置正确,并且开启map_meta_cap参数(这个是关键,它让WordPress正确处理权限检查)。

两种配置方式:

方式1:使用默认文章权限(简单快捷)

如果你不需要自定义权限,直接复用WordPress默认的文章权限即可,这样不用手动分配权限:

function register_my_custom_post_type() {
    $args = array(
        'public' => true,
        'label'  => '我的自定义文章类型',
        'capability_type' => 'post', // 复用默认文章的权限
        'map_meta_cap' => true, // 必须开启,确保权限检查正常
        // 其他CPT参数(比如supports、rewrite等)...
    );
    register_post_type( 'my_cpt', $args );
}
add_action( 'init', 'register_my_custom_post_type' );

方式2:自定义权限(适合需要精细权限控制的场景)

如果必须用自定义权限,那你需要完整配置capabilities数组,并且给用户角色分配这些权限:

function register_my_custom_post_type() {
    $args = array(
        'public' => true,
        'label'  => '我的自定义文章类型',
        'capabilities' => array(
            'edit_post'          => 'edit_my_cpt',
            'read_post'          => 'read_my_cpt',
            'delete_post'        => 'delete_my_cpt',
            'edit_posts'         => 'edit_my_cpts',
            'edit_others_posts'  => 'edit_others_my_cpts',
            'publish_posts'      => 'publish_my_cpts',
            'read_private_posts' => 'read_private_my_cpts',
            'delete_posts'       => 'delete_my_cpts',
            'delete_private_posts' => 'delete_private_my_cpts',
            'delete_published_posts' => 'delete_published_my_cpts',
            'delete_others_posts' => 'delete_others_my_cpts',
            'edit_private_posts' => 'edit_private_my_cpts',
            'edit_published_posts' => 'edit_published_my_cpts',
        ),
        'map_meta_cap' => true, // 必须开启
        // 其他CPT参数...
    );
    register_post_type( 'my_cpt', $args );
}
add_action( 'init', 'register_my_custom_post_type' );

// 给管理员角色分配自定义权限
function add_my_cpt_capabilities() {
    $admin_role = get_role( 'administrator' );
    $admin_role->add_cap( 'edit_my_cpt' );
    $admin_role->add_cap( 'read_my_cpt' );
    $admin_role->add_cap( 'delete_my_cpt' );
    $admin_role->add_cap( 'edit_my_cpts' );
    $admin_role->add_cap( 'edit_others_my_cpts' );
    $admin_role->add_cap( 'publish_my_cpts' );
    $admin_role->add_cap( 'read_private_my_cpts' );
    $admin_role->add_cap( 'delete_my_cpts' );
    $admin_role->add_cap( 'delete_private_my_cpts' );
    $admin_role->add_cap( 'delete_published_my_cpts' );
    $admin_role->add_cap( 'delete_others_my_cpts' );
    $admin_role->add_cap( 'edit_private_my_cpts' );
    $admin_role->add_cap( 'edit_published_my_cpts' );
}
add_action( 'admin_init', 'add_my_cpt_capabilities' );

第二步:过滤行操作,保留View和Trash并调整顺序

现在权限没问题了,接下来用post_row_actions钩子修改行操作,只保留View和Trash,并且把View放在前面:

function customize_cpt_row_actions( $actions, $post ) {
    // 只针对你的自定义文章类型,替换成你的CPT slug
    if ( $post->post_type !== 'my_cpt' ) {
        return $actions;
    }

    // 构建新的行操作数组
    $new_actions = array();
    
    // 保留View操作
    if ( isset( $actions['view'] ) ) {
        $new_actions['view'] = $actions['view'];
    }
    
    // 保留Trash/Delete操作(区分正常状态和回收站状态)
    if ( isset( $actions['trash'] ) ) {
        $new_actions['trash'] = $actions['trash'];
    } elseif ( isset( $actions['delete'] ) ) {
        $new_actions['trash'] = $actions['delete'];
    }

    return $new_actions;
}
add_filter( 'post_row_actions', 'customize_cpt_row_actions', 10, 2 );

把上面的代码放到你的主题functions.php或者自定义插件里,刷新后台就能看到效果了:行操作里只有View和Trash,而且View在前面~

内容的提问来源于stack exchange,提问作者NotoriousWebmaster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:16:11