首页 > 解决方案 > wordpress - 替换表中的元值

问题描述

我在数据库中有一个“usermeta”表。

并且有一些列,包括 meta_key 和 meta_value。

delivery_location(meta_key) 具有相同的 meta_value。

所以我想将当前的 meta_value 替换为 deliery_location 中的新值

(70982, 20, 'delivery_location', 'a:61:{i:0;s:47:"{lat:32.89030473256227, lng:-96.96264851172401}";i:1;s:47:"{lat:32.89359300394015, lng:-96.94936752319336}";i:60;s:0:"";}'),

我的问题是如何在 mysql 中一次替换所有相同的 meta_values?

在此处输入图像描述

标签: mysqldatabasewordpresskey-valuemeta

解决方案


您可以使用get_users()获取所有用户。检查下面的代码。代码进入您的活动主题functions.php 文件。

function update_all_user_delivery_location(){
    $users = get_users( array( 'fields' => array( 'ID' ) ) );

    foreach ( $users as $key => $user ) {

        $delivery_location = get_post_meta( $user->ID, 'delivery_location', true );

        // modify your code here

        update_post_meta( $user->ID, 'delivery_location', $delivery_location );
    }
}
add_action( 'init', 'update_all_user_delivery_location', 10, 1 );

推荐阅读