选择其他角色作为自定义帖子作者

时间:2021-06-11 作者:wesf90

我创建了自定义帖子类型(“组织”),我还创建了一个自定义用户角色(“代表”)。此外,我还确保代表角色具有读取/编辑/创建/发布帖子的功能。

然后,我以编程方式指定了一名代表作为;“作者”;组织的。

当我在组织概览屏幕(WP Admin>;Organizations)上时,我会在;“作者”;柱

但是,在编辑组织时;“作者”;下拉列表仅显示管理员/编辑器/贡献者。它不允许我查看/选择代表作为组织的作者。

我搜索过了,找不到答案。我做错了吗,或者有没有办法加入WP并让它识别其他角色?

编辑:以下是自定义帖子类型:

function cptui_register_my_cpts_organization_info() {

    /**
     * Post Type: Organizations.
     */

    $labels = [
        "name" => __( "Organizations", "hello-elementor-child" ),
        "singular_name" => __( "Organization", "hello-elementor-child" ),
        "menu_name" => __( "Organizations", "hello-elementor-child" ),
        "all_items" => __( "All Organizations", "hello-elementor-child" ),
        "add_new" => __( "Add New", "hello-elementor-child" ),
        "add_new_item" => __( "Add new Organization", "hello-elementor-child" ),
        "edit_item" => __( "Edit Organization", "hello-elementor-child" ),
        "new_item" => __( "New Organization", "hello-elementor-child" ),
        "view_item" => __( "View Organization", "hello-elementor-child" ),
        "view_items" => __( "View Organizations", "hello-elementor-child" ),
        "search_items" => __( "Search Organizations", "hello-elementor-child" ),
        "not_found" => __( "No Organizations found", "hello-elementor-child" ),
        "not_found_in_trash" => __( "No Organizations found in trash", "hello-elementor-child" ),
        "parent" => __( "Parent Organization:", "hello-elementor-child" ),
        "featured_image" => __( "Organization Logo", "hello-elementor-child" ),
        "set_featured_image" => __( "Upload Organization Logo", "hello-elementor-child" ),
        "remove_featured_image" => __( "Remove Organization Logo", "hello-elementor-child" ),
        "use_featured_image" => __( "Use as logo for this Organization", "hello-elementor-child" ),
        "archives" => __( "Organization archives", "hello-elementor-child" ),
        "insert_into_item" => __( "Insert into Organization", "hello-elementor-child" ),
        "uploaded_to_this_item" => __( "Upload to this Organization", "hello-elementor-child" ),
        "filter_items_list" => __( "Filter Organizations list", "hello-elementor-child" ),
        "items_list_navigation" => __( "Organizations list navigation", "hello-elementor-child" ),
        "items_list" => __( "Organizations list", "hello-elementor-child" ),
        "attributes" => __( "Organizations attributes", "hello-elementor-child" ),
        "name_admin_bar" => __( "Organization", "hello-elementor-child" ),
        "item_published" => __( "Organization published", "hello-elementor-child" ),
        "item_published_privately" => __( "Organization published privately.", "hello-elementor-child" ),
        "item_reverted_to_draft" => __( "Organization reverted to draft.", "hello-elementor-child" ),
        "item_scheduled" => __( "Organization scheduled", "hello-elementor-child" ),
        "item_updated" => __( "Organization updated.", "hello-elementor-child" ),
        "parent_item_colon" => __( "Parent Organization:", "hello-elementor-child" ),
    ];

    $args = [
        "label" => __( "Organizations", "hello-elementor-child" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "delete_with_user" => false,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => [ "slug" => "org", "with_front" => true ],
        "query_var" => true,
        "menu_icon" => "dashicons-networking",
        "supports" => [ "title", "editor", "thumbnail", "custom-fields", "author" ],
        "taxonomies" => [ "organization_services" ],
        "show_in_graphql" => false,
    ];

    register_post_type( "organization_info", $args );
}

add_action( \'init\', \'cptui_register_my_cpts_organization_info\' );

编辑此外,我确保;“代表性”;用户级别至少为1:

uMember User Role Editor

然而,在做出这些更改之后,我仍然没有看到代表在;“作者”;此自定义帖子类型的下拉列表。

我甚至手动添加wp_user_level 并将其设置为2 的内部wp_usermeta 在DB中,但仍然不起作用。

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

我已确保代表角色具有阅读/编辑/创建/发布帖子的能力

好的,很好。

但是你也分配了level_<number> 角色的能力,其中<number> 至少是1? 如果没有,则至少需要手动为角色中的用户分配级别1 或更高级别(2, 3, 等等)。

原因是,编辑后屏幕上的“作者”下拉列表只包括级别至少为的用户1 级别存储在名为wp_user_level 在非多站点安装中。

更具体地说,作者下拉列表使用wp_dropdown_users() (如果您的帖子类型使用旧/经典编辑器)或GET /wp/v2/users endpoint in the REST API (如果您的post类型使用Gutenberg,即在REST API中启用)使用who 参数设置为authors 其中仅包括用户级别为1 或以上。(see source on Trac)

因此,请确保代表角色具有适当的用户级功能,以便新用户将自动将上述元数据设置为正确的值。

对于该角色中的现有用户,您可以通过编程方式更新其wp_user_level 元数据(使用update_user_meta()), 或者,您可以使用原始SQL来更新该元数据,或者如果您的站点上安装了WP-CLI,那么您可以使用wp user meta update command 像这样:wp user meta update 123 wp_user_level 1 (其中123 是用户ID)。

更新以回复您的评论:

我之前创建的元字段是wp_user_level 在UserMeta表中。然而,这个元键必须包括WP表的prefix。因此,如果wp表前缀为wp_12 (意思是,您的usermetatable被命名为wp_12_usermeta), 然后wp_user_level meta键还必须具有前缀。所以,一旦我设定wp_12_user_level1,成功了!

是的,这是正确的,很抱歉没有澄清前缀:)

因此,用户级元的命名格式如下:<blog prefix>user_level, 其中blog prefix 形式为<base prefix><site ID>_ 或只是<base prefix>(请参阅下一段中的多站点注释),基本前缀默认为WordPress表前缀($wpdb->prefix), e、 g。wp_12_ 在您的情况下(请注意,前缀包括最后一个_).

因此,在WordPress安装中,表前缀默认为wp_, 将命名用户级元wp_user_level; 但是,如果启用了多站点,则名称(元密钥)将为wp_2_user_level 如果当前(子)站点的ID为2. 不过,主站点总是使用这种格式<table/base prefix>user_level, i、 e.无<site ID>_ 部分