admin管理员组文章数量:1435859
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
Share Improve this question asked Mar 21, 2019 at 12:21 Tuhin A.Tuhin A. 1757 bronze badges 4- Use 'wp_schedule_single_event()` and check user capabilities before assigning taxonomy inside your custom function. – Max Yudin Commented Mar 21, 2019 at 13:25
- I did, it return "false" – Tuhin A. Commented 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 Commented 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. Commented Mar 21, 2019 at 14:50
2 Answers
Reset to default 1So 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);
本文标签: Add custom taxonomy for post using cron job
版权声明:本文标题:Add custom taxonomy for post using cron job 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745667754a2669385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论