My aim is to insert a new draft post into the database via a custom front-end form I’ve created. I need the post title and content to be empty. Is this possible?
I’ve tried the following which doesn’t work:
$post_data = array(
'post_title' => '',
'post_type' => 'post',
'post_content' => '',
'post_status' => 'draft',
'post_author' => 1
);
$post_id = wp_insert_post( $post_data );
Note: You can create a new post using the back-end editor with empty title and content so I am wondering how they guys at WordPress do it.
You can not insert a blank post with
wp_insert_post
, WordPress will prevent it withwp_insert_post_empty_content
hook, you can see it in the source code : https://developer.wordpress.org/reference/functions/wp_insert_post/The only way to do it is to overpass this hook with a custom function
Here is an example (source)
There is a filter in WordPress core that can be used to prevent this.
https://developer.wordpress.org/reference/hooks/wp_insert_post_empty_content/
Which you should be able to use like: