将值从一个元键移动到另一个元键

时间:2012-02-27 作者:Nadine

迁移问题:

如何构造MySQL查询以将值从一个meta\\u键移动到另一个meta\\u键?

例如:我有一个旧的meta\\u键“guest\\u sort”,与post type“exhibitor\\u list”下的“posts”关联到“tf\\u exhibitor\\u sort”

我已经将帖子从原来的帖子类别移到了post\\u类型。还是说我运气不好,需要输入一些数据?

谢谢

4 个回复
最合适的回答,由SO网友:Nadine 整理而成

所以,朋友想出了一个剧本。您将其放在instal的根文件夹中,它会将值从一个元键翻转到另一个元键。我想我会分享它,以防其他人需要它。

 <?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.";

?>

SO网友:krembo99

我希望我能正确理解这个问题。移动帖子时,与之相关的元键和值应保留在帖子本身中,就像所有帖子元(包括附件、自定义图像等)―因此,保留这些值应该没有问题。通常,唯一应该做的事情是更改帖子的“post\\u type”属性。

但无论如何,如果您的问题是关于重命名元密钥,那么这应该是一个窍门:

update wp_postmeta 
set meta_key = \'new_key_name\' 
where meta_key = \'old_key_name\' 
当然,要更新值,可以使用相同的方法

UPDATE `wp_postmeta` SET `meta_value` = replace(meta_value, \'old_value\', \'new_value\') WHERE `meta_key` LIKE \'your_key\'
编辑1:我忘了提:**BACKUP YOUR DATABASE BEFORE ANY TRIAL **

编辑2:下面的评论:从一个字段复制到另一个字段(我对你的命名和你想要的值有点困惑,所以我发布了一般性的句子。

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

SO网友:helgatheviking

迪福备份您的数据!!这可能不是最有效的,但如果您更习惯于使用WP(像我一样),也可以使用WP函数而不是SQL查询来执行此操作

运行一次:

/*
 * 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;

}
您只需在重新加载主题一次后将其删除(并将该函数添加到初始化挂钩或其他内容)。或者你可以超级酷,使用我从Bainernet找到的“只运行一次”代码。

/*
* 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\');
}

SO网友:JoseLazo

您可以使用wp_schedule_single_event 运行一次。

其他方式可能:

 * 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\');```

结束

相关推荐

Grouping post-types in loop

当用户从下拉列表中选择特定类别时,将显示多个帖子类型的列表。我想用标题中的帖子类型标题分组显示数据。我不确定如何划分为岗位类型组。我花了一点时间寻找,但没有找到我真正需要的东西。我还试图添加<?php $post_type = get_post_type( $post->ID ); echo $post_type; ?> 但这只是重复(显然)。调用循环<div id=\"content\" role=\"main\" style=\"float:right; width:765px