aiosp_seometa_meta_key_convert

Convert old meta_key entries in the post meta table into new entries.

Description

aiosp_seometa_meta_key_convert( (string) $old = '', (string) $new = '', (bool) $delete_old = false ); 

First check to see what records for $new already exist, storing the corresponding post_id values in an array. When the conversion happens, ignore rows that contain a post_id, to avoid duplicate entries.

Parameters (3)

0. $old — Optional. (string) => ''
Old meta_key entries.
1. $new — Optional. (string) => ''
New meta_key entries.
2. $delete_old — Optional. (bool) => false
Whether to delete the old entries.

Usage

  1. if ( !function_exists( 'aiosp_seometa_meta_key_convert' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'all-in-one-seo-pack/admin/meta_import.php'; 
  3.  
  4. // Old meta_key entries. 
  5. $old = ''; 
  6.  
  7. // New meta_key entries. 
  8. $new = ''; 
  9.  
  10. // Whether to delete the old entries. 
  11. $delete_old = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = aiosp_seometa_meta_key_convert($old, $new, $delete_old); 
  15.  

Defined (1)

The function is defined in the following location(s).

/admin/meta_import.php  
  1. function aiosp_seometa_meta_key_convert( $old = '', $new = '', $delete_old = false ) { 
  2.  
  3. do_action( 'pre_aiosp_seometa_meta_key_convert_before', $old, $new, $delete_old ); 
  4.  
  5. global $wpdb; 
  6.  
  7. $output = new stdClass; 
  8.  
  9. if ( ! $old || ! $new ) { 
  10. $output->WP_Error = 1; 
  11.  
  12. return $output; 
  13.  
  14. // See which records we need to ignore, if any. 
  15. $exclude = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $new ) ); 
  16.  
  17. // If no records to ignore, we'll do a basic UPDATE and DELETE. 
  18. if ( ! $exclude ) { 
  19.  
  20. $output->updated = $wpdb->update( $wpdb->postmeta, array( 'meta_key' => $new ), array( 'meta_key' => $old ) ); 
  21. $output->deleted = $delete_old ? $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $old ) ) : 0; 
  22. $output->ignored = 0; 
  23.  
  24. } // Else, do a more complex UPDATE and DELETE. 
  25. else { 
  26.  
  27. foreach ( (array) $exclude as $key => $value ) { 
  28. $not_in[] = $value->post_id; 
  29. $not_in = implode( ', ', (array) $not_in ); 
  30.  
  31. $output->updated = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_key = %s WHERE meta_key = %s AND post_id NOT IN ($not_in)", $new, $old ) ); 
  32. $output->deleted = $delete_old ? $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $old ) ) : 0; 
  33. $output->ignored = count( $exclude ); 
  34.  
  35.  
  36. do_action( 'aiosp_seometa_meta_key_convert', $output, $old, $new, $delete_old ); 
  37.  
  38. return $output; 
  39.