block editor – Custom Post Type doesn’t have template content when creating programmatically in PHP but does when created in WP Admin


I’ve created a default template for a CPT for example…

// Define template
$template = [

// Register the CPT
        // ...
        'template' => $template,
        // ...

If I go into WP Admin and click new then the block editor is pre-populated with a heading block. However if I create a post programmatically in PHP…

$post_data = [
    'post_status' => 'draft',
    'post_title'  => 'Test post',
    'post_type'   => 'my_cpt',

return wp_insert_post( $post_data, false );

The post has no content.

Is there some way I can make this work? I don’t see a way of reusing my template. I can set post_content with parsed block data from the editor but I don’t know how to convert my template into actual blocks. I’d rather not have to manage the template in multiple places so reuse is key

Michael Elementary 1 year 2022-11-22T10:38:43-05:00 0 Answers 0 views 0

Leave an answer