$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>What am I doing wrong creating post draft via wp-cron? (wp_schedule_event & wp_insert_post)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

What am I doing wrong creating post draft via wp-cron? (wp_schedule_event & wp_insert_post)

matteradmin8PV0评论

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?

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?

Share Improve this question asked Mar 7, 2019 at 13:20 icantevenjustpostitPLEASEicantevenjustpostitPLEASE 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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' );
Post a comment

comment list (0)

  1. No comments so far