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: .1/src/wp-includes/post.php#L3784
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/browser/tags/5.1/src/wp-includes/post.php#L3784
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..
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($old_user);