0

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?

1 Answer 1

0

The problem is that you're only hooking the cron action on admin_init, which doesn't run when wp-cron.php is called. So the function will never run.

So this function shouldn't be inside wpcp_activate():

add_action( 'wpcp_cron_hook', 'wpcp_cron_do' );

Also, register_activation_hook() shouldn't be inside the admin_init hook either.

So your code should look like this:

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;
}

function wpcp_cron_activation() {
    if ( ! wp_next_scheduled( 'wpcp_cron_hook' ) ) :
        wp_schedule_event( time() , 'hourly', 'wpcp_cron_hook' ); // = daily
    endif;
}
register_activation_hook( __FILE__, 'wpcp_cron_activation' );

function wpcp_cron_do() {
    $post_title   = 'This should be the draft title';
    $post_content = 'All HERE';

    wpcp_make_post( $post_title, $post_content, 'post' );
}
add_action( 'wpcp_cron_hook', 'wpcp_cron_do' );
1
  • Thank you very much. I'll just add the init for reference: function wpcp_activate(){ //var_dump(time()); wpcp_cron_print_tasks(); } add_action( 'admin_init', 'wpcp_activate' ); It works now. Commented Mar 8, 2019 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.