I just encountered this very same problem. I was trying to do wp_insert_post()
from within a cron action, and the script would apparently just die during some save_post
action handler. I tracked down my problem, which turned out to be a save_post
handler which is used to save the POST data from a custom metabox. Within this handler, it calls check_admin_referer( 'myplugin_save', 'myplugin_nonce')
as a security check. This function very annoyingly calls wp_die()
if the security check fails, and as a result the execution just stops. So by adding a check to short-circuit the function if DOING_CRON
then everything started working as expected. See:
<?php
function myplugin_metabox_save(){
// If doing wp_insert_post() during a cron, the check_admin_referer() below
// will always fail and we know the metabox wasn't shown anyway, so abort.
if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ){
return;
}
// This is to get around the problem of what happens when clicking the "Add Post"
// link on the admin menu when browsing the main site.
if( $_SERVER['REQUEST_METHOD'] != 'POST' ){
return false;
}
// Verify this came from the our screen and with proper authorization
if ( !check_admin_referer( 'myplugin_save', 'myplugin_nonce') ) {
return false;
}
// ...
// Now handle $_POST fields and save them as postmeta for example
}
add_action( 'save_post', 'myplugin_metabox_save' );