So I've been googling and apparently a lot of people have struggled with this for the past X years and there doesn't seem to be any clear responses on Stack Overflow.
What I am developing is a plugin that grabs data via API from another publishing system and ideally creates post drafts in wordpress. When I call my wp_insert_post wrapper from plugin init, all works (and I end up with dozens of posts, as it goes). But when scheduled I just end up with no post, no error, just nothing.
function wpcp_make_post( $post_title, $post_content, $post_type ) {
$post_id = 0;
if ( post_type_exists( $post_type ) ) :
wp_set_current_user( 1 );
$to_post = array(
'post_type' => $post_type,
'post_status' => 'draft',
'post_author' => 1,
'post_title' => $post_title,
'post_content' => $post_content,
);
$post_id = wp_insert_post( $to_post );
endif;
return $post_id;
}
...
// way its wrapped
function wpcp_cron_activation() {
if ( ! wp_next_scheduled( 'wpcp_cron_hook' ) ) :
wp_schedule_event( time() , 'hourly', 'wpcp_cron_hook' ); // = daily
endif;
}
function wpcp_cron_do() {
$post_title = "This should be the draft title";
$post_content = "All HERE";
wpcp_make_post( $post_title, $post_content, 'post' );
}
...
// and called
function wpcp_activate(){
add_action( 'wpcp_cron_hook', 'wpcp_cron_do' );
register_activation_hook(__FILE__, 'wpcp_cron_activation');
wpcp_cron_activation();
//wpcp_cron_print_tasks();
}
add_action( 'admin_init', 'wpcp_activate' );
I am using the wp-cron.php?doing_wp_cron
parameter with the correct wp-config but I'm not even getting any errors.
Just no bloody post, ever.
define('WP_DEBUG', true);
define('DISABLE_WP_CRON', true);
So before I waste my time on some alternative draft creation, can anybody tell me what is it that I am doing wrong that it will not create a post?