2016-12-16 93 views
0

我開發了一個小的wordpress應用程序,它有研究所(wp用戶),培訓師(wp用戶),培訓生(wp用戶),課程(自定義文章)和通知(自定義文章)。所有的應用程序工作正常,但如果我刪除一個機構的所有信息屬於該機構像培訓師,實習生,通知&課程也應該刪除。當我創建「學院」用戶時,我在wordpress表'wp_users'和'institute name'中存儲所有相關信息,例如名字,姓氏在'wp_usermeta'中用'inistitute_name'作爲'user meta'像下表是我的代碼:如何刪除基於'user_meta'的所有用戶和帖子?

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

在創建教練(或)見習我的代碼如下所示:

$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」被刪除,那麼與該研究所相關的所有信息(如「培訓師」,「實習生」,「課程」&'通知')也應根據'user_meta'字段被刪除。可以刪除嗎? (或)我做錯了嗎?誰能告訴我我做錯了什麼?

回答

0

試試這個代碼:

$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知識。

請注意在測試此代碼之前請確保執行數據庫備份。

相關問題