Question

Migrating question:

How would I structure a MySQL query to move the value from one meta_key to another meta_key?

eg: I have an old meta_key of "guest_sort" associated with "posts" to "tf_exhibitor_sort" under the post type "exhibitor_listing"

I've already moved the posts from the original post category over to the post_type. Still possible, or am I outta luck and will have to do some data-entry?

Thanks.

Était-ce utile?

La solution 4

So, friend came up with a script for this. You put it in your root folder of your instal and it flips the values from one meta key to the other. I thought I'd share it in case anyone else need it.

 <?php
// Functions 
function db_connect($host,$db,$user,$pass){
  if(@mysql_connect($host, $user, $pass)){
    @mysql_select_db($db)or
    $content = sql_debug();
  }
  else {
    $content = sql_debug();
  }
  return @$content;
}

// Variables to change
$sql_username = "username";
$sql_password = "password";
$sql_host = "database host";
$sql_db = "database name";

// What is your post type that you want to limit this change to?
// IE: Only posts from 'exhibitors' will have this change
$post_type = 'post_type';

// What is the meta source? In this example it is website_link
// this is the "old" value you want to copy to the new value
$old_value = 'old_key';

// What is the meta destination? In this example lets use tf_ex_site_url
// BOTH key names must already exists in this instance of the script. If
// both don't exist connected with the post_id, the value wont be copied.
$new_value = 'new_key';

// Ok Go
db_connect($sql_host,$sql_db,$sql_username,$sql_password);
$counter = 0;
$query = "SELECT * from wp_posts WHERE post_type = '$post_type'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
  $post_id = $row['ID'];
  $query2 = "SELECT * from wp_postmeta WHERE post_id = '$post_id'";
  $result2 = mysql_query($query2);
  while ($row2 = mysql_fetch_array($result2)) {
    // Do update here
    if ($row2['meta_key'] == $old_value) {
      $counter++;
      $temp_new = $row2['meta_value'];
      $query_update = "UPDATE wp_postmeta SET meta_value = '$temp_new' WHERE meta_key = '$new_value' AND post_id = '$post_id'";
      $result_update = mysql_query($query_update);
      // Un-comment the below line if you want to BLANK the old value (ie in this case make website_link = to nothing after
      // updating the value of tf_exhibitor_sort
      // mysql_query("UPDATE wp_postmeta SET meta_value = '' WHERE meta_key = '$old_value' AND post_id = '$post_id'");
    }
  }
}

echo $counter." records updated.";

?>

Autres conseils

I hope I understand the question correctly . When you move a post , the meta keys and values associated with it should remain with the post itself, like all post meta (including the attachments , custom-image etc..) - so there should be no problem to keep the values. Normally ,The only thing that SHOULD happen is a change the 'post_type' property of the post.

but anyhow, if your question is about RENAMING meta keys , then this should dod the trick :

update wp_postmeta 
set meta_key = 'new_key_name' 
where meta_key = 'old_key_name' 

Of course, to update VALUES you can use the same approach

UPDATE `wp_postmeta` SET `meta_value` = replace(meta_value, 'old_value', 'new_value') WHERE `meta_key` LIKE 'your_key'

EDIT 1 : i forgot to mention : ** BACKUP YOUR DATABASE BEFORE ANY TRIAL **

EDIT 2 : following comment : To copy from one field to another (I got a bit confused with your naming and which value you want where , so I post the generic sentence.

UPDATE `TABLE` SET `FIELD2` = `FIELD1'

defo back up your data!! it might not be the most efficient, but you could also do this w/ WP functions instead of SQL queries if you are more comfortable speaking WP (like i am)

run this one time:

/*
 * Converts Old Content 
 */
function kia_convert_content(){
$products = get_posts(array('numberposts'=>-1,'post_type'=>'exhibitor_listing'));

foreach( $products as $post ) : setup_postdata($post); 

    // get old meta
    $test = get_post_meta($post->ID,'guest_sort', true);

    // update new meta
    update_post_meta($post->ID,'tf_exhibitor_sort',$test);

    // delete old meta
        delete_post_meta($post->ID, 'guest_sort');

    endforeach;

}

you can just delete it after you reload your theme once (and add that function to an init hook or something). or you could be super cool and use this 'run once' code i found from Bainternet.

/*
* run Once class
* http://en.bainternet.info/2011/wordpress-run-once-only
*/
if (!class_exists('run_once')){
    class run_once{
        function run($key){
            $test_case = get_option('run_once');
            if (isset($test_case[$key]) && $test_case[$key]){
                return false;
            }else{
                $test_case[$key] = true;
                update_option('run_once',$test_case);
                return true;
            }
        }

        function clear($key){
            $test_case = get_option('run_once');
            if (isset($test_case[$key])){
                unset($test_case[$key]);
            }
        }
    }
}




/*
 * convert the content exactly 1 time
 */

$run_once = new run_once;
if ($run_once->run('kia_convert_content')){
    add_action('init','kia_convert_content');
}

You can use wp_schedule_single_event to run once.

Other way maybe:

 * Run code only once
 */
function change_postkey_only_once()
{
    if (get_option('change_postkey_only_once_01') != 'completed') {
        global $wpdb;
        $query = "UPDATE " . $wpdb->prefix . "postmeta
                SET meta_key = 'new_key_name'
                WHERE meta_key = 'old_key_name'";
        $results = $wpdb->get_results($query, ARRAY_A);
        return $results;

        update_option('change_postkey_only_once_01', 'completed');
    }
}
add_action('admin_init', 'change_postkey_only_once');```
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top