Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › Problems creating new post with user having category/taxonomy ID restriction › Reply To: Problems creating new post with user having category/taxonomy ID restriction
OK… another update 🙂
I was able to confirm that the auto_assign_term function simply doesn’t work — for whatever reason. So I set about creating my own flow to do something similar, and therefore I was able to solve this issue as follows:
1) I registered a custom taxonomy called “ure”;
2) I added a term called “always” to this taxonomy — its term_id is 969
3) I added a function to functions.php that ensures that 969 is added to the list of IDs, if such list exists:
function rgb_add_term_969($user_id) {
$organizations = get_user_meta($user_id, 'wp_ure_categories_list', true);
if ($organizations!='' && get_user_meta($user_id, 'wp_ure_posts_restriction_type', true)=='1') {
$vals = explode(', ', $organizations);
if (!in_array('969', $vals)) {
$organizations = '969, ' . $organizations;
update_user_meta($user_id, 'wp_ure_categories_list', $organizations);
}
}
}
add_action('profile_update', 'rgb_add_term_969', 99);
4) I then added a function that always adds term 969 to a new post:
function rgb_allow_user_posts($post_id, $post, $update) {
global $pagenow, $wpdb;
if ($pagenow !=='post-new.php') { // for new added post only
return;
}
$terms_list_str = $this->user->get_categories_list();
if (empty($terms_list_str)) {
return;
}
$restriction_type = $this->user->get_restriction_type();
if ($restriction_type!=1) { // allow
return;
}
wp_set_object_terms( $post_id, 969, 'ure', false);
}
add_filter('wp_insert_post', 'rgb_allow_user_posts', 10, 3);
And this now works…
The difference is that you are using wp_set_post_terms — which is very precarious on how it treats IDs — rather than treating them as integers, it appears to sometimes treat them as a string, thus adding a new term whose name is 969 — rather than using the existing term_id 969. Using wp_set_object_terms seemed to help me here.
So now my users are able to add new posts, and they don’t have to set any taxonomy.
Thanks 🙂
Bira