2

I'm working on a website that assigns different roles to different users - and I'm looking a functionality I would probably best describe as 'reverse access control':

First, users of role "A" are allowed to publish content of type "A". If a user changes to role "B" he/she is not longer allowed to publish content of type "A".

Now I would want to find a way to 'unpublish' or 'hide' whatever that user has published while he/she was still assigned the user role "A". Currently I can think of the following two possible approaches:

  1. All posts are still published, but only the author and admins can see it.
  2. All posts are unpublished when the user role changes.

To me approach 1 sounds more practical – so in case the user switches back to role "A" everything would instantly be visible again.

I had a look at various plugins such as Members and User Role Editor but none seems to have that type of functionality – or am I missing something completely? Any pointers would be appreciated. Thank you!

2 Answers 2

2

Interesting functionality, you will need something that does this:

  • Will need to use the hook set_user_role to detect when user role change
  • Then will need to query all posts from that user
  • Then use wp_update_post to change the post status
3
  • Thank you! That sounds already quite promising… Although the more I think about it, the more I fear that approach 1) would be quite unpractical – for example if somebody actually has some hundreds of posts and only loses his role by accident… So maybe instead I should try to hook into the loop and only query posts by authors with role "A" or the like… Apr 14, 2016 at 16:49
  • 1
    Not really, because you will only change the post status. If user go back to his old role, just need to query all posts again and turn back the post status. Apr 15, 2016 at 13:37
  • Ah! RIght! Why wasn't I coming up with that?! Thank you! …will try that… Apr 15, 2016 at 15:33
1

option 1 is definitely easier and less prone to errors/accidents than changing publish settings every time an author changes roles.

To do so globally you should add a filter to the_content (and other content items in your page/post loops) which checks for the author's role and return some text to inform the viewer that the content is not currently available.

Your Answer

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

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