Add PHP block template to content using wp_insert_post

I’ve modified serialize_block a bit: function serialize_block_template( $block ) { $block_content=””; if (isset($block[2])) { $index = 0; foreach ( $block[2] as $chunk ) { $block_content .= is_string( $chunk ) ? $chunk : serialize_block_template( $block[2][ $index++ ] ); } } return get_comment_delimited_block_content( $block[0], $block[1], $block_content ); } Disclaimer: could have some bugs, only tested it for … Read more

Insert new term during new post creation

first you should set the taxnomy in custom post type you have just set only for post title post content post comments you did not set for custom taxnomy $post = array(‘tax_input’ => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty. ‘page_template’ => [ <string> ] // Requires … Read more

save_post + insert_post = infinite loop

This is because the first time you go round the loop $post is the current post. But the second time you go around the loop, $post has not changed. The same thing happens the 3rd, 4th, 5th, etc Because the $post variable is the current post of that page, not the post you’ve just saved/inserted, … Read more

Can we have a post without a slug?

I know this is really old question, but today I was dealing with something similar, and I have found a solution – if wp_unique_post_slug() calling is performance bottleneck, and post_name value is not important, then set post_name manualy to some random value. My wp_insert_post post action was taking too long lately (20-30s). It saves custom … Read more

When and Where to use wp_insert_post()

Use some type of conditional tag to check if those posts exist or not. If they do not exist, have them created with wp_insert_post. Do this logic inside the AddMyPages function and not around the add_action function. Example You want to add a page with a parent ID only if it does not exist, and … Read more

wp_insert_post incorrectly escapes HTML comments when they include tags

The following program: <?php $_SERVER[‘HTTP_HOST’] = ‘localhost’; require_once(‘wp-load.php’); $post = array( ‘post_title’ => ‘HTML Escape’, ‘post_content’ => ‘This is <!– a comment –><br/>This is <!– <p>a comment</p> –>’, ‘post_status’ => ‘publish’ ); $id = wp_insert_post($post); $post = get_post($id); var_export( esc_html( $post->post_content ) ); ?> Outputs the following in the browser: ‘This is <!– a comment … Read more