Some of the authors in my blog must always add their posts to a specific category each time they post.
Sometimes they forget this so therefore I’m wondering if there are any ways to automatically check and fix this.
Here’s an example of my desired outcome:
- Posts by Author X should always be assigned to category A
- Posts by Author Y should always be assigned to category B
- Posts by Author Z should always be assigned to category C
How do I achieve this?
The solution is quite simple. And is subdivided into two separate processes – querying the posts and modifying their category terms.
Querying the posts
get_posts
is a function that returns posts. A full list of arguments can be seen in WP_Query parametersTo get all posts for a specific author, you do something like:
Loop over each entry like so:
Setting category terms
wp_set_object_terms
is the function you’ll be looking at and using.Putting it together
I would probably go for something like this:
I’m sure there are ways in which you can further optimize this. Run this once to fix things. Then attach to
save_post
with just the set category part for future.I’m assuming you aren’t using custom post types. Below code successfully adds every new post (with publish_post action) to a specified category, excluding posts that are categorized with another specified category. You could simply tweak or remove the if statement to suit your specific needs.
You’ll want to use the
save_post
hook, which is fired every time a post is saved or updated.The callback should (optionally) check if a category is set (ignoring ‘uncategorized’) and otherwise set the category according to author using a author ID to category slug array.
The following is untested.
See @Soulseekah’s answer if you want to ‘fix’ already existing posts – although this method will work by updating existing posts.
The code above will ensure any future post is in at least one category (according to author)). Optionally,
publish_post
can be used to fire the callback function only when the post is saved and its status is publish, (thus for drafts the ‘correction’ won’t occur).check out my plugin which does just that
Author Category
You can set restrictions on individual users or roles too to post to a specific category using this plugin.
Edit: It’s really easy to use the plugin, it might be worth the time to check the link out.
However, i’ll still show you how to use it.
After installing the plugin, you can go into Settings > Restrict Categories to access its settings.
There you’ll see something like this:
As you can see, from here, you can decide which roles or users can post to which categories and stuff. When, say, you assign only the “University Update” category to the “Author” role, while posting a new post, the “Author” role users can only see the “University Update” category, i.e. the user can post only to that category. If you assign multiple categories, the the user can see multiple categories in the categories section while posting.
I hope that explains pretty much all about how to use it.