Skip to content
Advertisement

SQL: how to copy value of meta_key inside array in another meta_key?

I’m asking here since seems more like a SQL question than WordPress.

I have a WordPress installation using custom fields from a template and custom fields from a plugin (ACF). There are thousands of posts using the custom field from ACF, “subtitle”.

Now I need to move all “subtitle” values to the other custom field, “td_subtitle”. I found a simple answer in SQL being:

update wp_postmeta set meta_value = 'subtitle' where meta_key = 'td_subtitle';

But checking my database, the value of “td_subtitle” is inside an array, like this:

a:1:{s:11:"td_subtitle";s:24:"This is a post subtitle blablabla";}

“td_subtitle” is inside “td_post_theme_settings” meta_key.

enter image description here

Question: how to move all values from “meta_key > subtitle” to “meta_key > td_post_theme_settings > td_subtitle”?

Advertisement

Answer

You can use WP get_post_meta() and update_post_meta(). check the below code.

$td_post_theme_settings = get_post_meta( $post_id, 'td_post_theme_settings', true );

$td_post_theme_settings['td_subtitle'] = 'subtitle';

update_post_meta( $post_id, 'td_post_theme_settings', $td_post_theme_settings );

USEFUL LINKS

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement