3

I'm trying to insert posts using a cron job, but I keep running into errors where WP is missing functions (is_user_logged_in, wp_get_current_user).

Is there a proper way to run wp_insert_post using a cron job?

This is the code I have so far.

phrets_hourly is the WP_CRON hook

add_action('phrets_hourly', 'run_listings_update' );

function run_listings_update(){
  $fetch = new Fetcher();
  $fetch->fetch();
}

This is the code inside the $fetch object (Fetcher class) that is run.

public function fetch(){
  $options['class'] = 'RES';
  $options['limit'] = 10;
  $options['offset'] = 0;
  $options['silent'] = true;     
  $data = $this->query( $options );
  foreach ( $data as $datum ) {
    $this->place( $datum, true );
  }
}

public function place( $data, $silent ){
  $rets = $this->connect_to_rets();
  $listing = new Listing( $data );
  // check to see if listing already exists
  $post_id = $listing->check_listing_exists( $data['MST_MLS_NUMBER'] );
  if ( ! $post_id ) {
    $action = $listing->put();
  } else {
    $action = $listing->update($post_id);
  }
}

This is the method called from the $listing object (Listing class) that puts the data into the DB

public function put(){
  $title = $this->create_title( $this->data );
  // setup the 'post' data
  $post = array(
    'post_type' => 'listing',
    'post_title' => $title,
    'post_content' => $this->data['Remarks'],
    'post_status' => 'publish'
  );
  // add new meta data
  if ( ! is_wp_error( $post_id ) ) {
    $this->add_meta_data( $post_id );
    $this->assign_community( $post_id, $data );
  }
  // set the action
  $action = 'inserted';
  return $action;
}

Thanks!

9
  • 1
    Please add the relevant code that you've tried so far.
    – birgire
    Aug 19, 2014 at 3:40
  • Here is gist of the important stuff. gist.github.com/nathanaelphilip/1063641c3a8c8d84530d Aug 20, 2014 at 21:42
  • Did you check out this Trac ticket? There are some workarounds mentioned there, for example calling wp_set_post_terms() explicitly after wp_insert_post() with an empty tax_input argument.
    – birgire
    Aug 21, 2014 at 17:26
  • I think best way here is create a cron user, maybe with a custom role and custom capabilities, programmatically login that user using wp_signon inside cron and finally insert post and other stuff. If having that posts being published by that specific user is not a problem for you I think this is the easiest approach.
    – gmazzap
    Aug 21, 2014 at 18:38
  • There must be a lot more to this. You claim that running a WPCron task doesn't have basic WordPress functions like is_user_logged_in() and wp_get_current_user(), but they are nowhere in your code examples. Your code example also never attempts to actually insert the post.
    – BA_Webimax
    Aug 21, 2014 at 19:16

2 Answers 2

3

For those who stumble upon this later, use either option 1 or 2 found here: https://core.trac.wordpress.org/ticket/19373.

“For other developers who run into this and need to work around it, either of these 2 options work: call wp_set_post_terms() to add your taxonomies after calling wp_insert_post() set up a "current user" in your script before calling wp_insert_post()”

1

I found a way around this one.

Couldn't make sense of the core trac and it still hasn't been patched even though it's a 4 year old ticket. Here is the code I went with for inserting a custom post. The include_once directive is important here.

I should also note that the wp_set_auth_cookie(1) sets access rights for this script to the administrator. If you don't use an ID of 1 for your admin, then you'll need to update this. The function is also deprecated so you need to include the pluggable.php file.

//Add in the pluggable functions
include_once( ABSPATH . WPINC . '/pluggable.php' );
wp_set_auth_cookie( 1 );

$slug = strtolower("test-example");
$title = "Test Example";

//See if post exists or not then handle accordingly
$post_id = get_page_by_title( $title, OBJECT, 'cars' );

if( $post_id == NULL ) {
    $post_id = wp_insert_post(
        array(
            'comment_status'    =>  'closed',
            'ping_status'       =>  'closed',
            'post_author'       =>  1,
            'post_name'         =>  $slug,
            'post_title'        =>  $title,
            'post_status'       =>  'publish',
            'post_type'         =>  'cars'
        )
    );
} else {
    $post_id = $post_id->ID;
}

Simply change cars to whatever post type you want. The slug/title will also need to change accordingly.

Your Answer

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

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