WordPress 自定义 post 类型 - 无效 post 类型?

WordPress custom post type - Invalid post type?

为什么我得到 Invalid post type for:

function keyword_pages_init() {
    $args = array(
      'label' => 'Keywords',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'page',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'keyword'),
        'query_var' => true,
        'menu_icon' => 'dashicons-admin-page',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            // 'trackbacks',
            //'custom-fields',
            //'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',
            )
        );
    register_post_type( 'keyword', $args );
}
add_action( 'init', 'keyword_pages_init' );

出了什么问题?

但是带s的关键字没问题:

function keyword_pages_init() {
    $args = array(
      'label' => 'Keywords',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'page',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'keywords'),
        'query_var' => true,
        'menu_icon' => 'dashicons-admin-page',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            // 'trackbacks',
            //'custom-fields',
            //'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',
            )
        );
    register_post_type( 'keywords', $args );
}
add_action( 'init', 'keyword_pages_init' );

为什么!??

但它适用于新的函数名称!

function keyword2_pages_init() {
    $args = array(
      'label' => 'Keywordsx',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'page',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'keyword'),
        'query_var' => true,
        'menu_icon' => 'dashicons-admin-page',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            // 'trackbacks',
            //'custom-fields',
            //'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',
            )
        );
    register_post_type( 'keyword', $args );
}
add_action( 'init', 'keyword2_pages_init' );

为什么!!?

register_post_type( 'keyword', $args );

我认为关键字已被主题或保留关键字使用,您可以使用总和前缀更改此设置,例如 pre_keyword

如果不行,也换个 slug。

它会解决我认为的问题。我没有测试它,因为我超出了这里的测试范围。

自定义 post 类型 "keyword" 很可能已经注册。使用此函数列出所有已注册的 post 类型:

var_dump(get_post_types());

https://codex.wordpress.org/Function_Reference/get_post_types

基本上名称 "keyword" 是合法的 - 来自文档:

$post_type (string) (required) Post type. (max. 20 characters, cannot contain capital letters or spaces) Default: None

https://codex.wordpress.org/Function_Reference/register_post_type

如果您确定 slug 关键字 没有用于其他用途并且错误仍然存​​在,那么它也 可能是 WordPress 核心中的错误.如果你能找到什么报告。

特别是在过去的数百个 WordPress 项目中,我也遇到过这个问题一两次。

如果 post 类型已经存在 - 如何取消注册 post 类型的答案可以在这里找到(但是它可能已经被另一个插件或类似的原因注册了) : https://wordpress.stackexchange.com/questions/3820/deregister-custom-post-types

此外,您应该注意到 add_action 也有一个 $priority 参数。优先级可能很重要。

https://developer.wordpress.org/reference/functions/add_action/