WordPress taxonomy terms don’t insert when cron job executes wp_insert_post()

I have a PHP script that connects to a third-party application, retrieves data, and then inserts it into WordPress using wp_insert_post(). The data also has a custom taxonomy associated with it, which is being passed into the wp_insert_post() function via the $args array. Lastly, the script executes every hour via a cron job.

Here is the WordPress insert:

1
2
3
4
5
6
7
8
9
10
11
12
13
$args = array(
  'comment_status'  => 'closed',
  'ping_status'     => 'closed',
  'post_author'     => 1,
  'post_content'    => $content,
  'post_status'     => 'publish',
  'post_title'      => $title,
  'post_type'       => 'movie',
  'tax_input'       => array(
    'genre' => array('term1', 'term2', 'term3')
  )
);
$wp_movie_id = wp_insert_post($args);
$args = array(
  'comment_status'	=> 'closed',
  'ping_status'		=> 'closed',
  'post_author'		=> 1,
  'post_content'	=> $content,
  'post_status'		=> 'publish',
  'post_title'		=> $title,
  'post_type'		=> 'movie',
  'tax_input'		=> array(
    'genre' => array('term1', 'term2', 'term3')
  )
);
$wp_movie_id = wp_insert_post($args);

And here the cron job:

1
@hourly /usr/bin/curl --silent http://example.com/cron.php
@hourly /usr/bin/curl --silent http://example.com/cron.php

The problem was that every time the cron job ran, my custom taxonomy terms were missing, while everything else inserted just fine.

Whenever I executed the script manually from my browser though, the taxonomy terms were present, so I was sure that it was related to the cron job.

Turns out, the problem was within the wp_insert_post() function. Prior to inserting the taxonomy terms, WordPress checks whether that user has permission via current_user_can():

1
2
3
4
5
6
7
8
9
10
if(!empty($tax_input)) {
  foreach($tax_input as $taxonomy => $tags) {
    $taxonomy_obj = get_taxonomy($taxonomy);
    if(is_array($tags)) {
      $tags = array_filter($tags);
    }
    if(current_user_can($taxonomy_obj->cap->assign_terms)) {
      wp_set_post_terms($post_ID, $tags, $taxonomy);
    }
}
if(!empty($tax_input)) {
  foreach($tax_input as $taxonomy => $tags) {
    $taxonomy_obj = get_taxonomy($taxonomy);
    if(is_array($tags)) {
      $tags = array_filter($tags);
    }
    if(current_user_can($taxonomy_obj->cap->assign_terms)) {
      wp_set_post_terms($post_ID, $tags, $taxonomy);
    }
}

The cron job didn’t have authority to insert taxonomy terms. This also explains why it always worked in my browser, because I was logged in as an admin in the background.

Luckily there is a quick solution that solves this problem. Instead of inserting the taxonomy terms using the $args array, you can use another WordPress function called wp_set_object_terms() to perform the insert separately:

1
2
$terms = array('term1', 'term2', 'term3');
wp_set_object_terms($wp_movie_id, $terms, 'genre');
$terms = array('term1', 'term2', 'term3');
wp_set_object_terms($wp_movie_id, $terms, 'genre');

Hopefully this will save someone a couple hours of research!

9 thoughts on “WordPress taxonomy terms don’t insert when cron job executes wp_insert_post()

  1. jeroen

    Dude, really. This is the exacr same problem i had. This… this.. really 4 days of research. And this is it. Thank you so much for this solution !!!

    Reply
  2. Marcus

    Thanks man, I had the same issue! Working on it off and on all week and I was really confused as to what the difference was!

    Reply
  3. Jason

    I don’t usually comment when I find a solution. But this is literally the exact problem I had. And it definitely saved my hours worth of searching.

    Reply
  4. Pingback: WordPress не записывает значения таксономий при запуске скрипта через cron | Ablex блог

  5. James Revillini

    Thanks, dude. I knew it was a permissions thing but you saved me a ton of time figuring this out. I really don’t understand why an anonymous cron job would be allowed to insert a post but not use the standard insert array to attach taxonomies … that’s silly.

    Reply
  6. Derek Hauffe

    I’m experiencing exactly this problem now. Thanks for writing about it! I’ll make changes to my code and test it out. Again — thanks!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *