Tag Archives: Taxonomy

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:

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