Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save strangerstudios/a016b07c287800f8425f8aab2a456c15 to your computer and use it in GitHub Desktop.
Save strangerstudios/a016b07c287800f8425f8aab2a456c15 to your computer and use it in GitHub Desktop.
Unpublish an author's posts when their membership is cancelled.
<?php function pmpro_after_change_author_level_update_posts( $level_id, $user_id ) {
//get the user roles
$usermeta = get_userdata($user_id);
$user_roles = $usermeta->roles;
//check if the user is an author and is cancelling
if ( in_array( 'author', $user_roles ) && $level_id == 0 ) {
//get the user's posts
$args = array(
'author' => $user_id,
'post_type' => 'post',
);
$user_posts = get_posts( $args );
foreach ( $user_posts as $user_post ) {
$post = array( 'ID' => $user_post->ID, 'post_status' => 'draft' );
wp_update_post($post);
}
}
}
add_action( 'pmpro_after_change_membership_level', 'pmpro_after_change_author_level_update_posts', 10, 2);
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Set a Member Author’s Posts to Draft When Membership is Cancelled" at Paid Memberships Pro here: https://www.paidmembershipspro.com/set-member-authors-posts-draft-membership-cancelled/

@ChrisStudy
Copy link

Hi is there anyway we can put the posts back to published when they re-subscribed? will be batter if it is in code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment