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

Little short stacks of coins.
What do taxonomies and taxes have in common? They don't work. I kid, I kid.

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:

$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:

@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():

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:

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

Hopefully this will save someone a couple hours of research!

Featured image by Towfiqu Barbhuiya.


Comments (10)

Previously posted in WordPress and transferred to Ghost.

jeroen
August 3, 2012 at 12:01 pm

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 !!!

Marcus
September 5, 2012 at 2:28 pm

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!

J
November 20, 2013 at 4:47 am

Marry me. SERIOUSLY this bothered me for so long… you just cut my stress in half. THANK YOU.

Jason
February 23, 2014 at 8:27 am

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.

Petr Cibulka
June 5, 2015 at 10:09 am

You, sir, just saved my day. Thank you!!

James Revillini
December 2, 2015 at 9:54 pm

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.

Derek Hauffe
June 7, 2016 at 6:13 pm

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!

William
April 6, 2018 at 1:33 am

It’s impressive than 5 years after, this post save me few hours of research !
Thanks !

Eric
September 4, 2021 at 11:24 am

2021… still an issue. It’s really weird that wordpress even handles it differently based on cron.. like wtf