WordPress offers hooks to exclude or hide categories from the Post Editor in the admin area. You can remove the category meta box as well. Hiding categories with CSS is also possible. Here I will tell you different ways to hide categories in WordPress Admin Post Editor.
Years ago we used to write on one PHP framework that we have left working in present. So we didn’t want to post further articles in that framework as well as needed to hide them from the category meta box.
However, we didn’t want to stop them from appearing on public pages. So we searched on Google and came here with few code snippets to exclude or remove the categories. I’m mentioning them in the article here.
Hide Categories with CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php function hide_categories_by_css() { ?> <style type="text/css"> li#category-x { display: none; } </style> <?php } add_action( 'admin_head-post.php', 'hide_categories_by_css' ); add_action( 'admin_head-post-new.php', 'hide_categories_by_css' ); |
It’s a CSS rule and adds the display:none;
rule on post.php and post-new.php pages. Since it’s a CSS so you should be aware that the logged-in person can alter the CSS in the browser console.
Also visit: Remove menu items from WordPress Admin.
Exclude Categories in the Add New/Edit Post Screen
This is the function we had used. Add the following function either in the functions.php file of your theme or the plugin.
1 2 3 4 5 6 7 8 9 10 | function post_editor_list_terms_exclusions( $exclusions, $args ) { global $pagenow; if ( in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) ) { $exclusions = " {$exclusions} AND t.slug NOT IN ('slug-one','slug-two')"; } return $exclusions; } add_filter( 'list_terms_exclusions', 'post_editor_list_terms_exclusions', 10, 2 ); |
Here we’re first checking if current admin page is either post.php or post-new.php, using WordPress $pagenow global variable. If it is then we have modified the query using a NOT IN SQL clause to exclude categories by slugs. Add as many as category slugs inside the clause and those categories won’t appear in the category meta box.
Remove the Category/Tag Meta Boxes from the WordPress Post Editor
1 2 3 4 5 6 | function ft_remove_metaboxes() { remove_meta_box( 'categorydiv', 'post', 'normal' ); remove_meta_box( 'tagsdiv-post_tag', 'post', 'normal' ); } add_action( 'admin_menu', 'ft_remove_metaboxes' ); |
The remove_meta_box() WordPress Use the code snippet above to completely remove the Category/Tag Meta Box from the WordPress Post Editor screen. Also, replace the second parameter with the custom post type to hide Meta Boxes for that CPT.
So these above are 3 quick code snippets to hide or exclude categories in WordPress Admin Post Editor screen. While the first one hides them using CSS, the later is the WordPress approach doing the same. And the last one helps you removing the Category/Tag Meta Boxes.
Hope these snippets helped you hiding certain categories for the Post Editor. Do you have any other method to exclude WordPress categories from the admin screen? Do let us know in the comments.
Hello, i tried the snippet Exclude Categories in the Add New/Edit Post Screen and put in functions.php of my theme.
Instead of ‘slug-one’ i put the slug of the category i would like to exclude but the category is still showing when editing an existing post or adding new one.
Im using latest WP 5.5 stable release.
Hi Simone,
I will test the code with the latest version of WP sooner and let you know.
hi amit
it’s not working on gutenberg editor.
it’s working for classic editor.
hope for another updated snippet.
thank you.
Hi Jennie, I will check it again.
None of this work now. I mean in new WordPress version.
Hello Morshed,
I have the updated WordPress version 5.4.2 and the code snippet is working for me. I believe that you have missed placing the code correctly.