Huw Rowlands
Huw Rowlands

Reputation: 423

WordPress Query Loop Block not showing Custom Post Types

I have registered a new custom post type by hard coding it into my theme (yes, I could/should move this to a plugin I know!).

But when using the new Query Loop block, the only options under Post Type are 'posts' and 'pages'. Is there something missing from my CPT code causing this issue or could it be something else?

Thanks in advance.

<?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 );

Upvotes: 3

Views: 4081

Answers (1)

Huw Rowlands
Huw Rowlands

Reputation: 423

I resolved this by adding the following code to my custom post type function above:

'show_in_rest' => true,

More info here: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/

Upvotes: 8

Related Questions