WordPress 查询循环块不显示自定义帖子类型

Huw*_*nds 3 wordpress custom-post-type wordpress-gutenberg

我已经通过将其硬编码到我的主题中来注册了一个新的自定义帖子类型(是的,我可以/应该将其移动到我知道的插件中!)。

但是,当使用新的查询循环块时,帖子类型下的唯一选项是“帖子”和“页面”。我的 CPT 代码中是否缺少某些内容导致此问题,或者可能是其他原因?

提前致谢。

<?php
// Register Custom Post Type
function cpt_listings_function() {

    $labels = array(
        'name'                  => _x( 'Listings', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Listing', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Listings', 'text_domain' ),
        'name_admin_bar'        => __( 'Listings', 'text_domain' ),
        'archives'              => __( 'Listing Archives', 'text_domain' ),
        'attributes'            => __( 'Listing Attributes', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Listing:', 'text_domain' ),
        'all_items'             => __( 'All Listings', 'text_domain' ),
        'add_new_item'          => __( 'Add New Listing', 'text_domain' ),
        'add_new'               => __( 'Add New Listing', 'text_domain' ),
        'new_item'              => __( 'New Listing', 'text_domain' ),
        'edit_item'             => __( 'Edit Listing', 'text_domain' ),
        'update_item'           => __( 'Update Listing', 'text_domain' ),
        'view_item'             => __( 'View Listing', 'text_domain' ),
        'view_items'            => __( 'View Listings', 'text_domain' ),
        'search_items'          => __( 'Search Listing', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into listing', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Listing', 'text_domain' ),
        'items_list'            => __( 'Listings list', 'text_domain' ),
        'items_list_navigation' => __( 'Listings list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter listings list', 'text_domain' ),
    );
    $rewrite = array(
        'slug'                       => 'listing',
        'with_front'                 => true,
        'hierarchical'               => false,
    );
    $args = array(
        'label'                 => __( 'Listing', 'text_domain' ),
        'description'           => __( 'Property Listings Custom Post Type', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'author' ),
        'taxonomies'            => array(),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 100,
        'menu_icon'             => 'dashicons-admin-multisite',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => 'listings',
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'post',
        'rewrite'               => $rewrite,
        'capability_type'       => array( 'listing', 'listings' ),
        'map_meta_cap'          => true,
    );
    register_post_type( 'post_type_listings', $args );

}
add_action( 'init', 'cpt_listings_function', 0 );
Run Code Online (Sandbox Code Playgroud)

Huw*_*nds 7

我通过将以下代码添加到上面的自定义帖子类型函数中解决了这个问题:

'show_in_rest' => true,
Run Code Online (Sandbox Code Playgroud)

更多信息在这里:https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/