When a membership expires I would like that member’s post to be unpublished.
How do I do this? I am not using the woocommerce subscription plugin.
I have this code to run when a membership is deactivated – is this the same as expired?

    add_action('wc_memberships_deactivated', 'unpublish_post_expired_user');
    // when user's membership gets expired we want to unpublish this user
    function unpublish_post_expired_user($user_id){
    // check for expired memberships 

    if ( empty( wc_memberships_get_user_active_memberships($user_id) ) ) {
        // unpublish post

$user_membership = wc_memberships_get_user_membership( $user_id );

    $user_id = $user_membership->post->post_author;

    $args = array(
        'posts_per_page'   => -1,
        'post_type'        => 'therapist',
        'author'           => $user_id,
        'post_status'      => 'publish',
        'suppress_filters' => true
    );

    $therapists = get_posts( $args );

    foreach ($therapists as $therapist){

        $updated_post = $my_post = array(
            'ID'           => $therapist->ID,
            'post_status'   => 'draft'
        );
        wp_update_post($updated_post);
    }
}

    else { // do nothing
     }
    }

Read more here: woocommerce membership expires unpublist posts


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: