I have the following piece of code which publishes a user's post as long as they have purchased a subscription package and it changes the status of the post to draft as soon as their package has expired. I am just wondering how do I edit it so that the user can create a post and the post is automatically set to draft and then the status of the post is updated to published as soon as the user has completed the payment for a package? Any help would be greatly appreciated.

if ( ! in_array( $new_status, array( 'active', 'trial' ) ) ) {
        $user_limits = stm_get_post_limits( $user_id );

        $posts_args = array(
            'orderby'        => 'post_date',
            'order'          => 'DESC',
            'post_type'      => 'listings',
            'post_status'    => 'publish',
            'posts_per_page' => - 1,
            'meta_query'     => array(
                    'key'     => 'stm_car_user',
                    'value'   => $user_id,
                    'compare' => ''
            'fields'         => 'ids'

        $user_posts = get_posts( $posts_args );
        if ( count( $user_posts ) > $user_limits['posts_allowed'] ) {
            array_splice( $user_posts, 0, $user_limits['posts_allowed'] );
            foreach ( $user_posts as $user_post ) {
                $draft_post = array(
                    'ID'          => $user_post,
                    'post_status' => 'draft'
                wp_update_post( $draft_post );

        /*Change user back to private if not admin*/
        if(!user_can($user_id, 'manage_options')) {
            wp_update_user( array(
                'ID'   => $user_id,
                'role' => 'privateseller'
            ) );

    } else {
        /*If plan includes dealeship, change user role to dealer*/
        if ( $role == 'dealer' ) {
            wp_update_user( array(
                'ID'   => $user_id,
                'role' => 'stm_dealer'
            ) );

Have you considered the WooCommerce action woocommerce_order_status_completed which is called when the order status is changed to wc-completed? It is called with the order id and the order object (instance of WC_Order) from which you can get the details of the order.

  • I have this issue fixed using the method you suggested thank you. I just forgot to edit the question to say it was solved thanks. – Roger Alridge Jul 14 '17 at 0:11

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.