Is it possible to create a post using a metabox?
-
31-10-2019 - |
Question
I am trying to create a post (well, in the end, multiple posts) from a metabox using the 'save_post' hook. I executed the script once, creating 3900 or so new posts before I crawled into posts.php and discovered that wp_insert_post() calls the 'save_post' hook. Does anyone have a creative solution around this to create the post without doing a direct db insert (the less hacky the solution is the better).
Code
add_action( 'add_meta_boxes', 'my_metabox_init' );
add_action( 'save_post', 'my_metabox_save' );
function my_metabox_init()
{
add_meta_box(
'my-metabox',
'My Metabox',
'my_metabox_render'
'post',
'normal',
'core'
);
//enquque scripts and styles
}
function my_metabox_render() {
//generate datasets
}
function my_metabox_save() {
//data authenticity check
//process & sanitize data
//create posts
for( $i=0; $i<$count; $i++ ) {
$args = array(
'post_status' => 'pending',
'post_title' => $_POST['post_title'][$i],
'post_type' => 'custom_post_type',
);
foreach( $category_array[$i] as $category ) {
$args['tax_input']['custom-taxonomy'][] = $category;
}
if( $_POST['id_of_previously_created_post'][$i] != '' ) {
$args['ID'] = $_POST['id_of_previously_created_post'][$i];
unset( $args['post_status'] );
}
$new_post_id = wp_insert_post( $args );
}
}
Thanks!
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange