Advertisement
kkarpieszuk

remove duplicated attachments

Apr 30th, 2014
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.02 KB | None | 0 0
  1. global $wpdb, $sitepress;
  2.  
  3. $langs = 3; // set how many languages site has
  4.  
  5. $query = "SELECT ID, post_title, count(ID) AS cnt FROM {$wpdb->prefix}posts WHERE post_type LIKE 'attachment' GROUP BY post_title HAVING cnt > " .  $langs;
  6. $duplicated = $wpdb->get_results($query);
  7.  
  8. $issues = array();
  9. $i = 0;
  10. foreach ($duplicated as $duplicat) {
  11.    
  12.     $trid = $sitepress->get_element_trid($duplicat->ID, 'post_attachment');
  13.    
  14.     if (!$trid) {
  15.         continue;
  16.     }
  17.    
  18.    
  19.     $translations = $sitepress->get_element_translations($trid, 'post_attachment' );
  20.    
  21.     if (count($translations) < 1) {
  22.         continue;
  23.     }
  24.    
  25.     foreach ($translations as $lang => $tr) {
  26.         $issues[$i]['notin'][] = $tr->element_id;
  27.     }
  28.    
  29.     $issues[$i]['post_title'] = $duplicat->post_title;
  30.  
  31.     $i++;
  32. }
  33.  
  34.  
  35. foreach ($issues as $issue) {
  36. $notin = implode(",", $issue['notin']);
  37. $query = "DELETE FROM {$wpdb->prefix}posts WHERE `ID` NOT IN ($notin) AND `post_title` LIKE '" . $issue['post_title'] . "' AND `post_type` LIKE 'attachment'";
  38.  
  39. $result = $wpdb->query($query);
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement