I have created a custom post type and I have Woo Subscriptions. I would like to create a function that detects the post author role of each post.
If the author role is "Unsuscribed" then, change post status by that author to Draft. So with this, if users stops to pay the subscription, automatically their posts will not be shown.
Thank you!
CodePudding user response:
I think that you can use the add_user_role action hook. This hook fires after a user has been given a new role.
add_action( 'add_user_role', 'unpublish_non_subscriber_posts', 10, 2 );
function unpublish_non_subscriber_posts( $user_id, $role ) {
if ( $role !== 'unsubscribed' ) {
return;
}
$user_posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'your_custom_post_type',
'post_status' => 'publish',
'author' => $user_id,
'fields' => 'ids'
) );
if ( count( $user_posts ) === 0 ) {
return;
}
foreach ( $user_posts as $post_id ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'draft'
) );
}
}