1

I have developed a small wordpress application where It has Institutes(wp user), Trainers(wp user), Trainees(wp user), courses(custom post) and notifications(custom post). All the application is working fine but If I delete an institute all the information belongs to that institute like Trainers, Trainee, notifications & courses should also be deleted. While I was creating the 'institute' user I am storing the all the related info like first name, last name in wordpress table 'wp_users' and 'institute name' is storing as 'user meta' with key 'inistitute_name' in 'wp_usermeta' table like below is my code:

  $user_data = array(
            'ID' => '',
            'user_pass' => '',
            'user_login' => $first_name,
            'user_email' => $user_email,
            'first_name' => $first_name,
            'last_name' => $last_name,
            'role' => 'admin'//get_option('default_option')
        );
        $random_password = wp_generate_password(8,false);
        $user_id = wp_insert_user( $user_data );
        update_user_meta( $user_id, 'inistitute_name',$insititute_name );

While creating Trainer (or) Trainee my code is like below:

  $user_data = array(
               'ID' => '',
               'user_pass' => '',
               'user_login' => $first_name,
               'user_email' => $trainer_email,
               'first_name' => $first_name,
               'last_name' => $last_name,
               'role' => 'trainer'
         ); 
        $random_password = wp_generate_password(8,false);
        $user_id = wp_insert_user( $user_data );
        update_user_meta( $user_id, 'inistitute_name',$this->institute_name[0] );
        wp_set_password($random_password, $user_id);

While creating courses my code is like below:

  $user_ID = get_current_user_id();
  $institute_name = get_user_meta($user_ID, 'inistitute_name', true);
  $post = array(
            'post_title'    => $title,
            'post_content'  => $description,
            'post_status'   => 'publish', 
            'post_type' => "courses"  
        );
  $id = wp_insert_post($post);
  update_post_meta( $id, 'inistitute_name', $institute_name );

Suppose let us say if a institute 'abcd' is deleted then all the information like 'trainer', 'trainee', 'courses' & 'notifications' associated with that institute should also be deleted based on a 'user_meta' field. Is it possible to delete? (or) did I do any mistake? can anyone tell me what was I doing wrong?

Update:

  add_action( 'delete_user', 'cyb_delete_institute_info' );
function cyb_delete_institute_info( $id ) {

$user = get_userdata( $id );

if( ! empty( $user ) && in_array( "admin", (array) $user->roles ) ) {

    // Deleted user is admin, check `institute_name` meta field
    $institute_name = get_user_meta( $id, 'inistitute_name', true );

    if( ! empty( $institute_name ) ) {
        // Deleted user is an admin and has `institute_name` meta,
        // so, it is a "Institute" user

        // Get posts to delete
        $posts_args = array(
            'post_type' => 'notifications',
            'meta_query' => array(
                'key'   => 'inistitute_name',
                'value' => $institute_name
            ),
        );

global $wpdb;
$querystr = "DELETE FROM $wpdb->wp_dxwe_posts LEFT JOIN $wpdb->wp_dxwe_postmeta ON $wpdb->wp_dxwe_postmeta.post_id = $wpdb->posts.ID WHERE $wpdb->wp_dxwe_postmeta.meta_key = 'inistitute_name' AND 
        $wpdb->wp_dxwe_postmeta.meta_value = '$institute_name' AND $wpdb->wp_dxwe_posts.post_type = 'courses' ";
$wpdb->query( $querystr );

        $posts_args2 = array(
            'post_type' => 'courses',
            'meta_query' => array(
                'key'   => 'inistitute_name',
                'value' => $institute_name
            ),
        );

        $query = new WP_Query( $posts_args );
        $query2 = new WP_Query( $posts_args2 );
        $posts_to_delete = $query->get_posts();
        $posts_to_delete2 = $query2->get_posts();

        foreach( $posts_to_delete as $post ) {
            wp_delete_post( $post->ID );
        }

        foreach( $posts_to_delete2 as $post ) {
            wp_delete_post( $post->ID );
        }

        // Get users to delete
        $users_args = array(
            'role__in'       => array( 'trainer', 'trainee' ),
            'meta_query' => array(
                'key'   => 'inistitute_name',
                'value' => $institute_name
            ),
        );
        $query = new WP_User_Query( $users_args );
        $users_to_delete = $query->get_results();

        foreach( $users_to_delete as $user ) {
            wp_delete_user( $user->ID );
        }

    }

}
}

I was writing 2 separate WP_Query's to delete custom post_type's separately but din't work either. same result only notifications deleting correctly but for courses other institute courses also getting deleted.

7
  • Is "institute" a user role?
    – cybmeta
    Commented Dec 16, 2016 at 9:24
  • @cybmeta, No 'institute' is not a user role but for all institutes who were created will have a user role 'admin' as shown above in my code. Commented Dec 16, 2016 at 9:32
  • So, what is the difference between user types "Institute", "trainers", and so on. In posts you work with post types, in users what do you use if not roles? How do you identify a "Institute" user type?
    – cybmeta
    Commented Dec 16, 2016 at 9:34
  • The difference b/w 'Institute' & 'trainers' is that 'Institute' user will have a role 'admin' where as 'trainer' will have a user role 'trainer'. Commented Dec 16, 2016 at 9:39
  • And the difference between Institute and other admins is the institute_name meta field? I think you should assign "institute" a user role as well, but that is another question; working on an answer.
    – cybmeta
    Commented Dec 16, 2016 at 9:42

1 Answer 1

1

Because "institute" is a user and you want to perform some actions when it is deleted, you could use delete_user action (as we need information of the deleted user, deleted_user may not work, not sure about this but delete_user should work for sure). In that hook you can get all users and posts based on inistitute_name meta field and delete them.

Seeing your code, "Institute" user type can be identified by user role (you assing admin) and user meta inistitute_name. So, in delete_user action you can check if the deleted user is and admin and if it has institute_name assigned, in that case, the deleted user is a "institute" and you can get all other associated users and posts.

add_action( 'delete_user', 'cyb_delete_institute_info' );
function cyb_delete_institute_info( $id ) {

    $user = get_userdata( $id );

    if( ! empty( $user ) && in_array( "admin", (array) $user->roles ) ) {

        // Deleted user is admin, check `inistitute_name` meta field
        $institute_name = get_user_meta( $id, 'inistitute_name', true );

        if( ! empty( $institute_name ) ) {
            // Deleted user is an admin and has `institute_name` meta,
            // so, it is a "Institute" user

            // Get posts to delete
            $posts_args = array(
                'post_type' => array( 'courses', 'notifications' )
                'meta_query' => array(
                    'key'   => 'inistitute_name',
                    'value' => $institute_name
                ),
            );

            $query = new WP_Query( $posts_args );
            $posts_to_delete = $query->get_posts();

            foreach( $posts_to_delete as $post ) {
                wp_delete_post( $post->ID );
            }


            // Get users to delete
            $users_args = array(
                'role__in'       => array( 'trainer', 'trainee' ),
                'meta_query' => array(
                    'key'   => 'inistitute_name',
                    'value' => $institute_name
                ),
            );
            $query = new WP_User_Query( $users_args );
            $users_to_delete = $query->get_results();

            foreach( $users_to_delete as $user ) {
                wp_delete_user( $user->ID );
            }

        }

    }
}

Note: not tested, just written here as sample code.

12
  • I am trying your solution, testing the application now it will take time since as this is total application level testing. Commented Dec 16, 2016 at 11:30
  • I tried& tested your solution by deleting an institute, result is that all 'trainers', 'trainee' & 'notifications' deleted perfectly but 'courses' are deleting more i.e. not only beloved 'institute' courses but also other 'institute' courses getting deleted. can u plz once again check ur answer. Commented Dec 18, 2016 at 21:20
  • may be 'compare' field for 'meta_query' is missing in '$posts_args' array that is why it is deleting all other institute courses. Commented Dec 19, 2016 at 6:01
  • It is not missing, default value for compare argument is =, so the code should work for courses just like it works for the other custom post types. Can you try to make some debug and check the value of inistitute_name filed for the courses being deleted?
    – cybmeta
    Commented Dec 19, 2016 at 6:50
  • I did debug on 'inistitute_name' field for the courses being deleted & before deleting also. it is correct as I gave my institute name. Also checked for other institute courses they have their different insitute_name which they belongs to. Everything looking fine, I don't know whats going wrong! Commented Dec 19, 2016 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.