Genesis Tutorials

Delete Posts For Expired Subscribers When Role Changes

This tutorial provides the code which enables you to delete all posts ( and change capabilities ) for any user role if the subscribers role changes to customer.

The code has been written for a member based on this question :

I am using WooCommerce subscriptions. And members are able to create an entry to a custom post type from the front end if they have an active subscription. That works fine. However when they do not renew their subscription, their post remains on the site and we need to manually remove the post.The post can’t remain as the value of the subscription is in being able to have the post on the site (essentially, an entry in a directory on the front end). So I need a code which works along the lines of… If a subscriber’s subscription expires, THEN remove the post by that subscriber in custom post type “author”. When a person becomes a subscriber, they are given a new user role in WP “subscriber”. When their subscription expires they are reverted back to a standard role (called “customer”).

Video Demo

Shows how the subscriber can publish posts which are deleted when the subscriber role is changed to the customer role with read only capability.

Step 1: Here’s the 1st of 3 code snippets you can copy & paste to the end of your child themes functions file.

The code above assumes you are using WooCommerce with the subscription extension however if you’re not, you can use the following code to modify the default capabilities for a subscriber as well as add a new role for a customer. Also note, the code assumes use of the standard post_type post. Change the value for post_type for use with any custom post type.

Step 2:

The code above create a new role named customer with the read capability only.

Step 3 :

The code above modifies the default subscriber role so subscribers can edit, publish and delete their own posts.

Note : All posts will be deleted ( and saved in trash for 30 days ) as soon as the role changes to customer. Once the user role has changes to customer, the user will be allowed read only access. However, if the user changes back to a subscriber role, their posts can be restored from trash within a specific time frame. If you want to force delete, add a 2nd parameter true to wp_delete_post

Move Posts To Draft When Subscription Expires

If you would rather keep all users posts and move them to draft instead of delete them, use this code.

2 replies on “Delete Posts For Expired Subscribers When Role Changes”

Hi I have tried to use this but can not get it working. I have even tried this with WP default roles

can you assist please

Leave a Reply

Your email address will not be published. Required fields are marked *