I'm working on a site for a school and the teachers all want to post the homework for their students online, but only so each student can see his or her own class's homework posted by their teacher.

What i want to do is basically this:

  • get user role
  • if user role = rolename1, display posts by category 1, hide all other posts
  • if user role = rolename2, display posts by category 2, hide all other posts
  • and so on for the 22 different user roles

All of these user roles are subscriber levels, I just have them broken down this way as it was the easiest way to separate them into categories.

Ideally I want to have one blog page where the feed for all the posts for the site are displayed and you only see the ones that are appropriate for you within this feed. Everything else is hidden.

I also want to restrict each author to using only the categories I assign them rather than choosing from all the categories available.

Any ideas? Snippets or plugin recommendations would be highly appreciated.

  • Wouldn't it be easier to skip the category altogether, and display posts of a particular author based on a role?
    – Bram
    Commented Feb 2, 2019 at 10:11
  • 1
    Well, it would be, but some of the authors have more than one class so then their pages would show information for all of their classes on one page and we don't want anyone from one class to see info from any other classes.
    – sahiba25
    Commented Feb 4, 2019 at 19:45

2 Answers 2


The problem consists of three parts: getting the current user data, linking that to a category and retrieving and displaying the posts. You can include these snippets on a custom page template, which you assign to the 'My homework'-page.

Below these three sub-answers, I share some additional suggestions on ways of making this more user-friendly / user-manageable.

Getting the user data

This is done very easily using wp_get_current_user. This yields a WordPress User object, from which you can retrieve the roles using $current_user->roles (assuming you have set $current_user = wp_get_current_user();). This yields an array. I'm assuming the users only have one role, so that the first element in that array is the element we're checking against.

Linking users to categories

You can do this using an if-elseif-statement, as follows.

$roles = $current_user->roles;

if ($roles[0] == 'role'){
    $category_name = 'category-name';
}elseif ($roles[0] == 'other-role'){
    $category_name = 'other-category-name';

You can extend this as you like, by adding additional elseif-clauses. Replace role (and other-role) by the slugs of the roles, and category-name and other-category-name by the slugs of the categories.

Retrieving and displaying the posts

Using $category_name in the arguments of a WP_Query limits the result that query retrieves to the categories.

$qrt_args = array(
    'category_name' => $category_name

$posts = new WP_Query( $qry_args ); 

You can now use $posts in a regular loop and display the results.

Other ideas

Although the code above should work (haven't tested it), it requires you to edit code in order to make adjustments. That might be less convenient, for example if you at some point in time, are unavailable to make these changes.

Another option is to create a post for each student, subsequently assign the relevant category (or categories) to that post and set the author as the student. This way, you can match students and categories in the Wordpress Dashboard. Also, this provides a little more flexibility and robustness, for example to assign multiple categories to the same student. (The code posted above assumes only one category.) Of course, this will also display the post you've used to set up the match on the page with homework problems, but you can use that to your benefit ("Hey John, please find your homework problems below"). It is, however, also possible to prevent that from happening using an additional category or tag.

Another option, which is even nicer but also a bit more difficult to implement, is to create a new usermeta-field in which you save which categories of posts that user should be able to see.

Please note this does not (yet) provide a solution to this part of your problem:

I also want to restrict each author to using only the categories I assign them rather than choosing from all the categories available.

  • Thank you so much for writing such a detailed answer. The thing is I'm not super proficient in php or writing snippets for a template. I am using Avada theme, so what I ended up doing was using a combo of two plugins that worked out pretty well. Plugins are called RegistrationMagic and Restrict User Access.
    – sahiba25
    Commented Feb 7, 2019 at 21:39

You can try using WP Private Content Plus plugin. It allows you to restrict your content to specific user roles or or group of selected users. You just need to use the shortcode.

For multiple user roles, you have to use this shortcode:

[wppcp_private_content allowed_roles="subscriber,editor" ]
Private Content for Users with
Subscriber or Editor User Role
  • Thanks for the suggestion, I did try this, but for some reason it did not want to play nice with Avada, so I ended up using a combination of Registration Magic and a plugin called Restrict User Access. It's not a perfect solution because it won't hide the archive pages completely, but it does the job well enough for now.
    – sahiba25
    Commented Feb 4, 2019 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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