Site logo

Archived topic

Query Loop Block with CPT (3rd party) - Changing ''show_in_rest' on 'true'

3 replies · Started by Jens on November 23, 2022

Viewing posts 1–4 of 4

A plugin creates the custom post type 'dlp_document'. This supports taxonomies and tags. I use GenerateBlocks. In the query loop block, the custom post type 'dlp_document' is not listed.

In the plugin's code I found the registration of the custom post type. I changed the value 'show_in_rest' to 'true' manually as a test. Then it works.

What do I have to put in the function.php for this change?


	const POST_TYPE_SLUG = 'dlp_document';

	
	public function register() {
		add_action( 'init', [ $this, 'register_post_type' ], 15 );
		add_action( 'init', [ $this, 'flush_rewrite_rules' ], 16 );
	}

	/**
	 * Register the Document post type.
	 */
	public function register_post_type() {
		$labels = [
			'name'                  => _x( 'Documents', 'Post Type General Name', 'document-library-pro' ),
			'singular_name'         => _x( 'Document', 'Post Type Singular Name', 'document-library-pro' ),
	
			'menu_position'       => 26,
			'show_in_admin_bar'   => true,
			'show_in_nav_menus'   => true,
			'can_export'          => true,
			'has_archive'         => false,
			'hierarchical'        => false,
			'exclude_from_search' => false,
			'show_in_rest'        => false,
			'publicly_queryable'  => true,
			'capability_type'     => 'post',
			'rewrite'             => [ 'slug' => $this->document_slug ],
		];

		register_post_type( self::POST_TYPE_SLUG, $args );
	}

Hi there,

you can try using the register_post_type_args filter hook:

https://developer.wordpress.org/reference/hooks/register_post_type_args/

The provide an example in the comments, and you should be able to repurpose that like so:


add_filter('register_post_type_args', 'dlp_document_show_in_rest', 10, 2);
function dlp_document_show_in_rest($args, $post_type){

    if ($post_type == 'dlp_document'){
        $args['show_in_rest'] = 'true';
    }

    return $args;
}

Thank you very much. It works.

Glad to hear that!

This archived topic is closed to new replies.