Open DietteJanssen opened 5 years ago
+1
+1
+1
+1
+1
+1
+1
Here's a dirty workaround that worked with my case. You create a page template and put your default template blocks in it. Then you do that
$query = new WP_Query(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template-defalut.php',
'post_type' => 'page',
'fields'=>'ids',
));
$posts = $query->posts;
if(empty($posts)){return;}
$content = get_post_field('post_content', $posts[0]);
wp_update_post(array(
'ID'=>478,
'post_content'=>$content
));
For me it worked as I wanted to publish many posts at once with a certain template blocks, I hope that would help until this feature is applied.
I didn't test it with core blocks, but the below method works fine with ACF blocks (so I would think it does with core stuff as well).
function cpt_mac_submenus() {
$labels = array( //... );
$args = array(
// ...
'template' => array(
array( 'acf/submenus--title-and-description', array(
'lock' => array(
'move' => true,
'remove' => true,
),
) ),
array( 'acf/submenus--output-navigation', array(
'lock' => array(
'move' => true,
'remove' => true,
),
) ),
array( 'acf/submenus--single-image', array(
'lock' => array(
'move' => true,
'remove' => true,
),
) ),
),
'template_lock' => 'all',
);
register_post_type( 'mac-submenus', $args );
}
add_action( 'init', 'cpt_mac_submenus' );
$content = '<!-- wp:acf/submenus--title-and-description /--><!-- wp:acf/submenus--output-navigation /--><!-- wp:acf/submenus--single-image /-->';
$new_submenu_post_args_Arr = array(
'post_title' => $title,
'post_type' => 'mac-submenus',
'post_status' => 'draft',
'post_author' => 1,
'post_content' => $content,
);
wp_insert_post( $new_submenu_post_args_Arr );
Basically, the $content
variable should be set up with "blank" blocks, no content, no configuration, just the skeleton - as a result, the output is 100% the same thing configured in the register_post_type()
arguments array.
i believe wp_insert_post needs to be extended to have knowledge of the template. This is something the wp core team should do.
https://developer.wordpress.org/reference/functions/wp_insert_post/
the wp_insert_post has arg for page_template
but this is not the same as a gutenberg template.
so either use the registered gutenberg template, if no content is inserted or add means to merge data with the blocks inside the gutenberg template :)
ref:
https://github.com/WordPress/gutenberg/issues/13163#issuecomment-1588634802
https://developer.wordpress.org/reference/functions/parse_blocks/
https://developer.wordpress.org/reference/classes/wp_block_parser_block/
https://developer.wordpress.org/reference/functions/serialize_block/
https://developer.wordpress.org/reference/functions/serialize_blocks/
i have searched through the issues/related issues and i have been unable to find a solution... i need to include the gutenberg registered template when creating a new post programmatically.
i have registered a custom post type "credits" via php which includes the following arguments:
i am unable to programmatically add a new post which includes the gutenberg "template" array.
i have been able to output the following:
... which pulls in the gutenberg paragraph block successfully (rather than pulling it into the "classic" editor). however, i need it to pull in the template of blocks the same way it does when adding a new post from the wordpress backend.
for context, using php i have hooked into learndash:
... i need to find a solution to achieve this, even if it requires using a different method ( other than wp_insert_post ) to achieve the desired result. any help or direction would be appreciated.