0

I want to hide the categories 'news','blog','main', but not 'bloggers' from contributors users only in Add New/edit post area.

I found this code here which works with Members plugin:

add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
function yoursite_list_terms_exclusions( $exclusions, $args ) {

global $pagenow;
if (in_array($pagenow,array('post.php','post-new.php')) && 
!current_user_can('see_special_cats')) {
    $exclusions = " {$exclusions} AND t.slug NOT IN ('slug-one','slug-two')";
}
return $exclusions;
}

They say, it works, but I don't know how to customize it. Simply adding in function.php doesn't work.

0

You'll have to change see_special_cats to something fitting. As you want to exclude contributors, but allow all others, you could use publish_posts (See the codex for all capabilities of contributors).

Your code should be something like

add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
function yoursite_list_terms_exclusions( $exclusions, $args ) {

    global $pagenow;
    if (in_array($pagenow,array('post.php','post-new.php')) && 
!current_user_can('publish_posts')) {
        $exclusions = " {$exclusions} AND t.slug NOT IN ('news', 'blog', 'main')";
    }
    return $exclusions;
}

Keep in mind that you might have to adjust the slugs, which I've just assumed are news, blog and main.

  • I have the categories with these slugs. Now I must add 'publish_posts' as a custom capability for contributors?... I just want to hide all other categories instead of 'bloggers'. – Merkucio Feb 4 '18 at 11:55
  • No, this is working for those who cannot publish_posts, so you shouldn't add that capability to contributors, as it'll affect only contributors (and readers, but those cannot add posts). If you have other categories besides bloggers, you'll have to include those - it might be easier to generate the array of slugs in PHP in that case. – janh Feb 4 '18 at 12:42

Your Answer

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

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