This guide shows you how to restrict an author to publishing posts only in specific categories in WordPress. By default, any user with the Author role can create posts in any category. This restriction is useful for multi-author sites where each writer covers a dedicated topic. You’ll need an administrator account and the ability to add a code snippet to your theme’s functions.php file or a custom plugin.
What You’ll Need
- WordPress admin access
- FTP or file manager access to your theme’s functions.php file (or use a code snippet plugin)
- Basic understanding of PHP (or just copy-paste the provided code)
Step-by-Step
- Decide which categories each author can use. Go to Posts > Categories in your WordPress admin. Note the category IDs (you can see them when you hover over a category name in the list – the ID appears in the link, e.g.,
tag_ID=5). Alternatively, you can use the category slug. - Back up your site. Before editing any code, make a full backup of your files and database. This ensures you can restore if something goes wrong.
- Create a custom function. You’ll add a PHP function that hooks into WordPress to filter the categories shown in the post editor and to prevent saving posts in disallowed categories. Here’s a basic version to place in your theme’s
functions.phpfile or a custom plugin:
// Restrict authors to specific categories
function restrict_author_categories( $query ) {
if ( is_admin() && current_user_can( 'author' ) ) {
$allowed_categories = array( 5, 7 ); // Replace with your category IDs
$query->set( 'cat', implode( ',', $allowed_categories ) );
}
}
add_action( 'pre_get_posts', 'restrict_author_categories' );
// Hide disallowed categories in the post editor
function hide_categories_for_author() {
if ( current_user_can( 'author' ) ) {
$allowed_categories = array( 5, 7 ); // Replace with your category IDs
echo '<script>jQuery(document).ready(function($) {
var allowed = ' . json_encode( $allowed_categories ) . ';
$("#categorychecklist input[type=checkbox]").each(function() {
var id = $(this).val();
if ( allowed.indexOf( parseInt(id) ) === -1 ) {
$(this).prop("disabled", true).parent().hide();
}
});
});</script>';
}
}
add_action( 'admin_footer-post.php', 'hide_categories_for_author' );
add_action( 'admin_footer-post-new.php', 'hide_categories_for_author' );
// Prevent saving posts with disallowed categories
function prevent_save_disallowed_categories( $post_id ) {
if ( current_user_can( 'author' ) ) {
$allowed_categories = array( 5, 7 ); // Replace with your category IDs
$categories = wp_get_post_categories( $post_id );
$disallowed = array_diff( $categories, $allowed_categories );
if ( ! empty( $disallowed ) ) {
// Remove disallowed categories
wp_remove_object_terms( $post_id, $disallowed, 'category' );
}
}
}
add_action( 'save_post', 'prevent_save_disallowed_categories' );
- Adjust the code for your needs. Replace
array( 5, 7 )with the actual category IDs you want to allow. You can also use slugs by modifying the code accordingly. - Save the file. If you’re using a child theme, add the code to its
functions.php. If you’re using a code snippet plugin, create a new snippet and activate it. - Test with an author account. Log in as an author (or create a test author) and try to create a new post. You should see only the allowed categories in the category checklist. Try to save a post with a disallowed category (if you can manage to select one via URL manipulation) – it should be automatically removed.
Common Mistakes
- Using the wrong user capability. The code uses
current_user_can('author'), which checks if the user has the ‘author’ role. If you want to restrict editors or contributors, change this to ‘editor’ or ‘contributor’. - Hardcoding category IDs incorrectly. Make sure you’re using the correct category IDs. Double-check by hovering over the category name in the admin list.
- Not using a child theme. If you add the code to a parent theme’s functions.php, it will be lost when the theme updates. Always use a child theme or a custom plugin.
- Forgetting to handle the ‘quick edit’ screen. The script only works on the post editor screen. Quick edit may still show all categories. To handle that, you’d need a more complex script.
- Not considering the ‘uncategorized’ default. If an author doesn’t select any category, WordPress assigns the default ‘Uncategorized’ category. Make sure that category is in the allowed list, or the post will be saved without a category.
Tips
- Use a code snippet plugin like Code Snippets to manage the code without editing theme files. It’s safer and easier to disable if needed.
- Consider using a plugin like ‘User Role Editor’ or ‘Capability Manager Enhanced’ to restrict capabilities more broadly, but note that these don’t directly restrict categories by default.
- Test thoroughly with different user roles to ensure the restriction works as expected.
- Add a fallback for the ‘quick edit’ screen by also disabling the category dropdown there, if you need to.
- Log out and log back in after making changes to ensure the new code is loaded.
Troubleshooting
- The category list still shows all categories. This usually means the JavaScript isn’t loading. Check that you’ve added the script correctly and that jQuery is available (it is in WordPress admin). Also, make sure the
admin_footerhooks are correct. - Authors can still save posts with disallowed categories. The
save_posthook should remove them, but if you’re seeing it fail, check that the function is actually being called (add a log or error message). Also, ensure the category IDs are correct and that the post’s categories are being retrieved properly. - The site shows a white screen after adding the code. This is a PHP syntax error. Check for missing semicolons, brackets, or quotes. If you can’t access the admin, use FTP to remove the code.
- Authors can’t see any categories at all. This happens if the allowed categories array is empty or if the IDs are wrong. Verify that the category IDs exist and that the array is not empty.
- The restriction doesn’t apply to existing posts. This code only affects new posts or posts being edited. Existing posts remain unchanged. You may need a bulk edit to reassign categories.
Frequently Asked Questions
Can I restrict authors to multiple categories?
Yes, just add more category IDs to the array, e.g., array( 5, 7, 12 ).
Will this work for editors as well?
Yes, change current_user_can('author') to current_user_can('editor') in the code.
Can I use category slugs instead of IDs?
Yes, but you’d need to convert slugs to IDs in the code. It’s simpler to use IDs.
Does this prevent authors from creating new categories?
No, this code only restricts which categories they can assign to posts. Authors typically don’t have the capability to create categories anyway.
Will this affect the REST API or block editor?
The code uses the classic editor hooks. For the block editor (Gutenberg), you may need additional JavaScript to filter categories. The save_post hook still works, so posts with disallowed categories will be cleaned up, but the UI may not hide them.
Can I restrict a specific author, not all authors?
Yes, you can check the user ID instead of the role. For example, if ( get_current_user_id() == 3 ).
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Wireless Controller Charging: Can You Play While Plugged In?
- Secure a Node.js API: Layered Checklist
- Zoom Desktop Client: Features, Setup, and Fixes
- Python Integer Identity: Why It Varies by Run
- Can Technology Solve Poverty? Facts & Limits
- Cannot Detect Device? Fixes for USB & Bluetooth