Advertisement
Chouby

Polylang DB correction for duplicate tags and categories

Oct 1st, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Polylang database correction
  4. */
  5.  
  6. /*
  7. Correct a the database when tags and/or categories are duplicated with Polylang activated (an not with Polylang deactivated)
  8. See: http://wordpress.org/support/topic/plugin-polylang-categories-and-tags-are-duplicated
  9. Polylang must be activated for this plugin to work
  10.  
  11. Make a backup of your database
  12. Copy paste in a file correct.php
  13. Upload the file in the plugins directory
  14. Activate the plugin -> it should immediately remove duplicate tags and categories
  15. Deactivate the plugin and delete it
  16. */
  17.  
  18. add_action('init', 'pll_correct_db', 100); // after Polylang
  19. function pll_correct_db() {
  20.     global $polylang, $wpdb;
  21.     if (!isset($polylang))
  22.         return;
  23.  
  24.     $meta_ids = $wpdb->get_col("SELECT meta_id FROM $wpdb->termmeta AS tm WHERE tm.meta_key = '_language'");
  25.     $terms = $wpdb->get_col("SELECT term_id FROM $wpdb->termmeta AS tm WHERE tm.meta_key = '_language'");
  26.     $unique = array_unique($terms, SORT_NUMERIC);
  27.     foreach (array_keys(array_diff_key($terms, $unique)) as $key)
  28.         $to_correct[] = $meta_ids[$key];
  29.  
  30.     if (isset($to_correct)) {
  31.         $query = $wpdb->prepare("DELETE FROM $wpdb->termmeta WHERE meta_id IN( ". implode( ',', $to_correct ) ." )");
  32.         $wpdb->query($query);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement