Title: Loading a default pattern per custom post type
Last modified: March 28, 2024

---

# Loading a default pattern per custom post type

 *  [tshingleton](https://wordpress.org/support/users/tshingleton/)
 * (@tshingleton)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/)
 * I want to be able to load a specific custom pattern when creating a post (e.g.
   of a specific post type). I’ve gotten as far as the following:
 *     ```wp-block-code
       function load_post_type_patterns() {
           // Define an initial pattern for the 'Book' post type
           $post_type_object = get_post_type_object( 'book' );
           $post_type_object->template = array(
               array(
                   'core/block',
                   array(
                       'ref' => 2603, // The ID of my custom pattern
                   ),
               ),
           );
       }
   
       add_action( 'init', 'load_post_type_patterns' );
       ```
   
 * This works, but I have to manually select the pattern and select “Detach”. Otherwise,
   my edits on the page will update the pattern (even though I have “Sync” disabled).
   This is too counterintuitive for my editors.
 * Anyone solve for this?

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [mdshak](https://wordpress.org/support/users/mdshak/)
 * (@mdshak)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-17536735)
 * You can load a specific custom pattern automatically when creating a post of 
   a specific post type, you should use the `block_editor_settings` filter to set
   the initial pattern for the desired post type. Here’s how you can modify your
   code to get desire result.
 *     ```wp-block-code
       function load_post_type_patterns( $editor_settings, $post ) {
           // Define an initial pattern for the 'Book' post type
           if ( 'book' === $post->post_type ) {
               $editor_settings['__experimentalFeatures']['unfilteredTemplates']['book_template'] = array(
                   array(
                       'title' => 'Custom Book Pattern',
                       'content' => array(
                           array(
                               'core/block',
                               array(
                                   'ref' => 2603, // The ID of your custom pattern
                               ),
                           ),
                       ),
                   ),
               );
               $editor_settings['template'] = 'book_template';
           }
   
           return $editor_settings;
       }
   
       add_filter( 'block_editor_settings_all', 'load_post_type_patterns', 10, 2 );
       ```
   
 * This code adds a custom pattern named `book_template` for the ‘Book’ post type.
   When creating a new post of the ‘Book’ type, this pattern will be loaded automatically.
   Adjust the post type and pattern ID as needed for your setup.
 *  Thread Starter [tshingleton](https://wordpress.org/support/users/tshingleton/)
 * (@tshingleton)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-17537961)
 * Thanks [@mdshak](https://wordpress.org/support/users/mdshak/)! This got me very
   close. A few things:
   – I had to reference ‘$post->post->post_type’ instead of‘
   $post->post_type’ in the conditional, as the latter threw an undefined error —
   this appears to be working now– I’m getting the WSOD (wp_debug on, no errors 
   printed on screen) if I’m using the full code snippet. I commented out the line
   of code that I determined is the culprit. Running as is of course means the code
   does nothing; I need to uncomment and solve for that line if no one can beat 
   me to it!– Confirmed that ‘2601’ corresponds to the ID of a pattern on my siteThank
   you again for the help! Anyone interested in taking a shot at solving this would
   be appreciated!Here’s the code right now:
 *     ```wp-block-code
       /*
       * Load custom patterns per post type
       */
   
       function load_post_type_patterns( $editor_settings, $post ) {
   
           if ( 'book' === $post->post->post_type ) { // Define an initial pattern for the 'Book' post type
               $editor_settings['__experimentalFeatures']['unfilteredTemplates']['book_template'] = array(
                   array(
                       'title' => 'Book Template',
                       'content' => array(
                           array(
                               'core/block',
                               array(
                                   'ref' => 2601, // The ID of your custom pattern
                               ),
                           ),
                       ),
                   ),
               );
               //$editor_settings['template'] = 'book_template';
           }
   
           return $editor_settings;
       }
   
       add_filter( 'block_editor_settings_all', 'load_post_type_patterns', 10, 2 );
       ```
   
    -  This reply was modified 2 years, 2 months ago by [tshingleton](https://wordpress.org/support/users/tshingleton/).
 *  [leifer](https://wordpress.org/support/users/leifer/)
 * (@leifer)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-17543054)
 * 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-block-code
       <!-- 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.
 *     ```wp-block-code
       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.
 *  Thread Starter [tshingleton](https://wordpress.org/support/users/tshingleton/)
 * (@tshingleton)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-17546433)
 * [@leifer](https://wordpress.org/support/users/leifer/) Thanks for sharing the
   workaround! I may have to go this route, if I can’t find a solution here.
 * Ideally, would be great to allow editors to edit patterns on the backend and 
   having that update as appropriate, vs using the importer.
 *  [leifer](https://wordpress.org/support/users/leifer/)
 * (@leifer)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-17548881)
 * Welp, my PM didn’t like my solution either, but his loss is your gain because
   I took another look at this issue 🙂
   Instead of getting the lowcode from my “
   default content” Pattern by-hand… I just do it programmatically by Post ID of
   my Pattern then return that. Works a mint. I feel both smart and dumb now. I 
   hope WP doesn’t do anything to break this functionality, because using Patterns
   as “pseudo-templates” in this way is super powerful.
 *     ```wp-block-code
       function prefix_filter_book_content($content,$post) {
           if ( $post->post_type === 'book' ) {
               $my_pattern_id = 5055;
               $content_pattern = get_post($my_pattern_id);        
               $content = $content_pattern->post_content;
           }
           return $content;
       }
       add_filter( 'default_content', 'prefix_filter_book_content', 10, 2 );
       ```
   
    -  This reply was modified 2 years, 2 months ago by [leifer](https://wordpress.org/support/users/leifer/).
 *  Thread Starter [tshingleton](https://wordpress.org/support/users/tshingleton/)
 * (@tshingleton)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-17549539)
 * [@leifer](https://wordpress.org/support/users/leifer/) 
   This is exactly what 
   I was looking for! Implemented and works great!Thanks for taking the time to 
   figure this one out and share
 *  [Mike Johnston](https://wordpress.org/support/users/wmjohnston06/)
 * (@wmjohnston06)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-17718988)
 * I just implemented [@leifer](https://wordpress.org/support/users/leifer/)’s revised
   code. Worked perfectly the first time. Thanks for publishing it!
 *  [SB](https://wordpress.org/support/users/sairah/)
 * (@sairah)
 * [1 year, 10 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-17924173)
 * I’ve spent a lot of time trying to accomplish _just_ exactly what [@leifer](https://wordpress.org/support/users/leifer/)’
   s last code snippet does flawlessly.
 * One note for any others who struggled like me: the pattern post ID you have to
   dig into your database for. WordPress treats patterns as posts, technically, 
   so you have to look at the timestamps in the post table and description to see
   when you saved the pattern. Or you can check the postmeta table also, which has
   less data in it so will probably be quicker. Once you have that post ID, you’re
   golden.
 * Thanks for sharing this simple way of accomplishing what I was trying to dig 
   into Block Bindings API as a non-coder for!
 *  [jasonlampada](https://wordpress.org/support/users/jasonlampada/)
 * (@jasonlampada)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-18231873)
 * Hi there, this is exactly what I am looking for but I am unsure how to implement
   the code onto my site and get it working. Where would I put the code snippet 
   provided by [@leifer](https://wordpress.org/support/users/leifer/)
 *  [jezthomp](https://wordpress.org/support/users/jezthomp/)
 * (@jezthomp)
 * [1 year, 3 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-18349639)
 * Could you not just do this with another post’s content as well?
   Setup a ‘master’
   page that you’re happy with, get that page/post id then re-use its content each
   time you create a new post/page?
 * Anything wrong with this method?
 *     ```wp-block-code
        add_filter('default_content', 'cpt_portfolio_default_content', 10, 2);function cpt_portfolio_default_content($content, $post) {    if ($post->post_type === 'cpt-portfolio') {        // Replace 5055 with the ID of your reusable block or draft page        $pattern_post_id = 12559;        $pattern_post = get_post($pattern_post_id);        if ($pattern_post) {            $content = $pattern_post->post_content;        }    }    return $content;}
       ```
   

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Loading a default pattern per custom post type’ is closed to new replies.

## Tags

 * [thanks](https://wordpress.org/support/topic-tag/thanks/)
 * [tip](https://wordpress.org/support/topic-tag/tip/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 10 replies
 * 7 participants
 * Last reply from: [jezthomp](https://wordpress.org/support/users/jezthomp/)
 * Last activity: [1 year, 3 months ago](https://wordpress.org/support/topic/loading-a-default-pattern-per-custom-post-type/#post-18349639)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
