[英]copy an existing meta_value to a new meta_key name - WordPress
I am trying to copy a meta_key and the corresponding meta_value to a new row in the wp_postmeta
table where the meta_key gets a new name but with the original meta_value
.我正在尝试将 meta_key 和相应的 meta_value 复制到
wp_postmeta
表中的新行,其中 meta_key 获得新名称但具有原始meta_value
。
I can copy one post with MySQL but this needs to be done for more than 300 posts so there must be a way to do this in a statement.我可以用 MySQL 复制一个帖子,但是这需要为 300 多个帖子完成,因此必须有一种方法可以在语句中执行此操作。
Example given: meta_key: postalCode meta_value: 1234AA needs to be copied to for the corresponding post id to meta_key: wpcf-adres meta_value:1234AA给出的例子:meta_key: postalCode meta_value: 1234AA 需要复制到对应的post id 到meta_key: wpcf-adres meta_value:1234AA
I have tried: update wp_postmeta set meta_key = 'postalCode' where meta_key = 'wpcf-adres'我试过:更新 wp_postmeta set meta_key = 'postalCode' where meta_key = 'wpcf-adres'
but this, of course, does not duplicate and keep the original meta_key but just renames it.但这当然不会复制并保留原始的 meta_key,而只是重命名它。
I hope someone can guide me in the right direction.我希望有人能指导我朝着正确的方向前进。
Thanks a lot for looking into it.非常感谢您的调查。
Kind regards亲切的问候
Wim维姆
update wp_postmeta set meta_key = 'postalCode' where meta_key = 'wpcf-adres'
This isn't my code, I saved it previously but it may be what you're after.这不是我的代码,我之前保存了它,但它可能是你想要的。 Switching the keys as needed.
根据需要切换键。
<?php
// HOOK ON REGISTERING NEW USER/CUSTOMER
add_action('user_register', 'mm_sync_phone_number' , 20, 1);
// HOOK ON PERSONAL OPTIONS UPDATE
add_action('personal_options_update', 'mm_sync_phone_number' , 20, 1);
// HOOK ON USER PROFILE UPDATE
add_action('edit_user_profile_update','mm_sync_phone_number' , 20, 1);
function mm_sync_phone_number( $user_id ) {
// GET PHONE NUMBER FROM OTHER FIELD
$phone_number = get_user_meta( $customer_id, 'meta_key_of_other_phone_field', true );
// UPDATE PHONE NUMBER IN BILLING PHONE NUMBER FIELD
update_user_meta( $user_id, 'billing_phone', $phone_number );
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.