I am trying add custom taxonomy with post using cron job. The problem is since wordpress 4.7, it verify if current user have capability of assigning taxonomy. Crob job don't have the capability.

I am using this for registering custom taxonomoy

add_action( 'init', 'create_locations_hierarchical_taxonomy', 0 );

function create_locations_hierarchical_taxonomy() {

// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI

  $labels = array(
    'name' => _x( 'Locations', 'taxonomy general name' ),
    'singular_name' => _x( 'Location', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Locations' ),
    'all_items' => __( 'All Locations' ),
    'parent_item' => __( 'Parent Location' ),
    'parent_item_colon' => __( 'Parent Location:' ),
    'edit_item' => __( 'Edit Location' ), 
    'update_item' => __( 'Update Location' ),
    'add_new_item' => __( 'Add New Location' ),
    'new_item_name' => __( 'New Location Name' ),
    'menu_name' => __( 'Locations' ),

// Now register the taxonomy

  register_taxonomy('location',array('post'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'location' ),

And this script to assign taxonomy array with $post_arr

        $post_location = array_map('intval', $post_location);   
            if ($post_type == 'post') {
                $post_arr['tax_input'] = $post_location;

Currently, my script can create custom taxonomy but can't assign with post..

Ref: https://core.trac.wordpress.org/browser/tags/5.1/src/wp-includes/post.php#L3784

  • Use 'wp_schedule_single_event()` and check user capabilities before assigning taxonomy inside your custom function.
    – Max Yudin
    Mar 21, 2019 at 13:25
  • I did, it return "false"
    – Tuhin A.
    Mar 21, 2019 at 14:15
  • Your code does not have any schedule or capability check, only taxonomy registration, which is completely useless from the perspective of the question.
    – Max Yudin
    Mar 21, 2019 at 14:21
  • This is just a part of my code, obviously this is not the full script. It's a part of auto crawler script that do it's job every 5/10/15.. minutes as I defined. As this is automated process, I don't have to check user capabilities. There is no logged in user at the moment of crawling. However I can set user id equal to the admin, as @mrben522 suggest in another answer. That didn't help too.
    – Tuhin A.
    Mar 21, 2019 at 14:50

2 Answers 2


So after 3 days of research I got this only one way. Before inserting post, there is no way to assign tax_input cause that will verify user capability. Cron job don't have that.

$new_id = wp_insert_post($post_arr, true);
Post Inserted, now we can add custom taxonomy with the post like this

$status = wp_set_object_terms($new_id, $term_id, 'location');

here location is the term slug. Funny thing is, cron can add post/taxonomoy, but can't assign taxonomy without capability check!!..Someday someone will get this helpful..

  • This day, some one did find it helpful. Thanks Mate.
    – Shahbaz A.
    Feb 2 at 14:04

The problem is that there may not be a current user when that cron job runs. so you need to check if there is a logged in user, save their ID if so, then set the current user to whatever ID has the correct capabilities, do your stuff, then set it back to whatever it was before. Something like this (untested code, don't throw this anywhere important without testing it)

$old_user = get_current_user_id();
wp_set_current_user(1); //use whatever ID you want here
    //do your stuff
$post_location = array_map('intval', $post_location);
if ($post_type == 'post') {
    $post_arr['tax_input'] = $post_location;
  • wp_set_current_user(1); doesn't do the trick. current_user_can( $taxonomy_obj->cap->assign_terms ) still return 'fasle' though user id 1 is admin. And if I write log for wp_set_current_user(1), output is like this {"data":{"ID":"1","user_login":"admin##","user_pass":"#############","user_nicename":"nicename","user_email":".... I think It don't set the current user id as 1.
    – Tuhin A.
    Mar 21, 2019 at 14:45
  • try checking what get_current_user_id() returns after you've called wp_set_current_user(1).
    – mrben522
    Mar 21, 2019 at 14:49
  • oh it's returning 1!
    – Tuhin A.
    Mar 21, 2019 at 14:53
  • but still current_user_can( $taxonomy_obj->cap->assign_terms ) returns false
    – Tuhin A.
    Mar 21, 2019 at 14:54
  • I'm not sure what the issue is then. I'd hook into has_cap() and check what role it thinks the current user has, as well as what it's trying to check against at that point.
    – mrben522
    Mar 21, 2019 at 14:59

Your Answer

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

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