Site logo

[Resolved] Query Loop Block with CPT (3rd party) – Changing ”show_in_rest’ on ‘true’

Home Forums Support [Resolved] Query Loop Block with CPT (3rd party) – Changing ”show_in_rest’ on ‘true’

Home Forums Support Query Loop Block with CPT (3rd party) – Changing ”show_in_rest’ on ‘true’

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2428913
    Jens

    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 );
    	}
    
    #2428944
    David
    Staff
    Customer Support

    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;
    }
    #2428958
    Jens

    Thank you very much. It works.

    #2429047
    David
    Staff
    Customer Support

    Glad to hear that!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.