PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] how to delete all users and posts based on 'user_meta'?

 July 08, 2022     posts, wordpress     No comments   

Issue

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?


Solution

I got the solution form wordpress.stackexchange.com link to that question is here

https://wordpress.stackexchange.com/questions/249435/how-to-delete-all-users-and-posts-based-on-user-meta/



Answered By - Prasad Patel
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing