I have a custom taxonomy 'siteSections' that is registered for both posts and users. The idea was to limit certain sections of posts/pages to specific users, by assigning terms to pages and assigning those same terms to the users who should edit them. I thought using roles wouldn't be a great idea, because what I am defining is actually the same type of role (author), but for different groups of pages.

Everything was going smoothly until I wanted to restrict access for users to use only the terms of siteSections that they were assigned to. i.e. if termA and termB are assigned to User1, User1 should only be able to assign termA and termB to page1, but not termN.

I have been trying to impliment the following code, inspired by https://wordpress.stackexchange.com/a/173369/103031:

add_filter('get_terms', 'restrict_siteSections');
function restrict_siteSections($siteSection, $user_id) {    

    global $pagenow;

    // If we are in the new/edit post page and not an admin, then restrict the categories
    if( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow )
        return $siteSection;

    if ( !is_admin() )
        return $siteSection;

    if ( current_user_can('administrator') || current_user_can('editor') ) // check for user capabilities
        return $siteSection;

    //get the terms that the user is assigned to 
    $curUser = wp_get_current_user();
    $assigned_terms = wp_get_object_terms( $curUser->ID, 'siteSection' );
    $siteSection = $assigned_terms;
    return $siteSection;

This doesn't work, and after some debugging, I think the problem is wp_get_object_terms. Is this function unavailable inside of get_terms? I guess I also can't use get_terms or get_the_terms since I am hooking into get_terms?

All the answers I have seen on stack exchange assume the terms to be restricted are constant, or else they use roles.

I thought the idea of having one taxonomy for both users and posts was elegant, but am I working myself into a corner? Is there a better and simpler way?

  • WordPress doesn't have what you need out of the box. Terms can't be restricted to specific users. You will need completly custom coded solution, interface to assign terms to users. It can be done, but it is not simple. – Milan Petrovic Sep 9 '17 at 9:29
  • I haven't shown you all my code. I have a working solution for everything I need except this one final piece. I have a taxonomy registered to posts, users, and media. I have everything set up for assigning the terms of the taxonomy to users. I have posts hidden from edit.php unless they meet the users' taxonomy terms. The only thing left, I think, is to fix the checkbox of terms they can assign on post.php/new_post.php. I can post the rest of my code if that helps. – kavon Sep 10 '17 at 10:23
  • From my experience with this, the best course is to completely replace the metabox for that taxonomy, and render your own where you can control list of terms to display. – Milan Petrovic Sep 10 '17 at 10:26
  • @MilanPetrovic I have been looking into making my own metabox. I used the 'meta_box_cb' argument when registering my taxonomy and pulled this code to build the metabox. I am trying to filter the available categories but it looks like I have to hook into wp_terms_checklist, and I don't quite see how. I've found this, but as far as I can tell, that removes ticks, not boxes. Thoughts? – kavon Sep 23 '17 at 4:30
  • You will not be able to use any WP function as is because none of them is made to do exactly what you need. You will need to build metabox and function to render checkboxes from scratch. – Milan Petrovic Sep 23 '17 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.