如何删除基于 'user_meta' 的所有用户和帖子?

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

我开发了一个小型 wordpress 应用程序,其中有学院(wp 用户)、培训师(wp 用户)、学员(wp 用户)、课程(自定义 post)和通知(自定义 post).所有应用程序都运行良好,但如果我删除一个机构,所有属于该机构的信息,如培训师、实习生、通知和课程也应该被删除。当我创建 'institute' 用户时,我存储了所有相关信息,如 wordpress 中的名字、姓氏 table 'wp_users' 和 'institute name' 存储为 'user meta' 键 'inistitute_name' in 'wp_usermeta' table 如下是我的代码:

  $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 );

在创建 Trainer(或)Trainee 时,我的代码如下所示:

$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);

创建课程时我的代码如下:

 $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 );

假设我们说,如果一个机构 'abcd' 被删除,那么与该机构相关的所有信息,如 'trainer'、'trainee'、'courses' 和 'notifications' institute 也应该根据 'user_meta' 字段删除。是否可以删除? (或)我做错了吗?谁能告诉我我做错了什么?

试试这个代码:

$user_args = array(
    'meta_key' => 'inistitute_name',
    'meta_value' => 'your_value', //<-- replace it by your user_meta value.
    'meta_compare' => '=',
    'orderby' => 'ID',
    'number' => -1
);
// Custom query.
$user_query = new WP_User_Query($user_args);

// Get query results.
$users = $user_query->get_results();

//print_r($users);
// Check for users
if (!empty($users))
{
    // Loop over users.
    foreach ($users as $user)
    {
        $user_id = $user->ID;
        // Get each user's data.
        $user_info = get_userdata($user->ID);

        $user_display_name = $user_info->display_name;

        $args = array(
            'numberposts' => -1,
            'post_type' => 'any', //<-- you can Specific type by array('post', 'my_custom_post')
            'author' => $user_id
        );

        // get all posts by this user: posts, pages, attachments, etc..
        $user_posts = get_posts($args);

        if (!empty($user_posts))
        {
            // delete all the user posts
            foreach ($user_posts as $user_post)
            {
                wp_delete_post($user_post->ID, true);
            }
        }
        wp_delete_user( $user_id, $reassign );
    }
}
else
{
    //"no user found"
}

根据我的 WP 知识,我还没有测试过这段代码。

请注意,在测试此代码之前请务必备份数据库。

我得到了这个问题的解决方案 wordpress.stackexchange.com link 在这里

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