I ran into a similar issue and found this thread looking for the answer myself. I was not able to find a way to inject (and automatically detach) a pattern. I don’t know why, but I remade several Patterns and made sure to unsync them, but none-the-less when added the way you are adding them above they behaved as though they were still synced.
So, I didn’t find a way to add a Pattern to a Custom Post Type (and detach it).
However, I did find a similar work around.
Get your “lowcode” from your Pattern. Manage Patterns -> Find your Pattern -> Export as JSON.
Take the value of the “content” of that JSON, that’s your “lowcode”.
Example WordPress “lowcode” for a Pattern with two Paragraph blocks and two Heading blocks.
<!-- wp:paragraph -->
<p>My book paragraph.</p>
<!-- /wp:paragraph -->
<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading">Book Heading</h4>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>Another Book paragraph</p>
<!-- /wp:paragraph -->
<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading">Another Heading</h4>
<!-- /wp:heading -->
In my case, I had to do some hand-editing to remove newline characters and such, but eventually got ‘clean’ lowcode that I could then use to inject into the “content” of the post type template.
function prefix_filter_book_content( $content, $post ) {
if ( $post->post_type === 'book' ) {
$content ='<!-- wp:paragraph -->
<p>My book paragraph.</p>
<!-- /wp:paragraph -->
<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading">Book Heading</h4>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>Another Book paragraph</p>
<!-- /wp:paragraph -->
<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading">Another Heading</h4>
<!-- /wp:heading -->';
}
return $content;
}
add_filter( 'default_content', 'prefix_filter_book_content', 10, 2 );
And, though the setup is a bit “hacky” the end result works. The end-user, when they create a new “Book” custom post type, they will get the “Default” Blocks added to their Editor. But since they are added as Blocks, and not as a Pattern they are treated as page / post content and saved normally without needing to detach anything.
Thank you for listening to my TED talk.