If a user has contributor role (or role of my choice) then delete his posts older than x minutes. How to?

  • Subscribers cannot write posts. – cjbj May 18 '18 at 20:53
  • Well it was just a example for a user role. I edited my post. – ANdy May 18 '18 at 21:02

This is a simple question, but it involves quite some work, so I'll just give you an outline:

First, you need to define a cron job that runs every minute or so. This will allow you to define an action that is run automatically. Beware that this will run only when the site is visited, because otherwise WP is just a bunch of files that sit on your server's hard drive. Now, you have a hook that fires every minute when your site is indeed visited every minute, but less if you get less visits.

Second, you will have to write a function that deletes posts based on the user role of its author. Get all users with a certain role using get_users. Feed the result in a query in which you also specify posts older than the X minutes your want. Then loop through all retrieved posts and delete them.

If you're actually going to do this, you're putting quite a load on your server, so don't expect it to be fast. Doing this max every hour might be a better idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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