$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>advanced custom fields - Change post value in WordPress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

advanced custom fields - Change post value in WordPress

matteradmin8PV0评论

How change existing value to new and keep data ?

I changed the name in ACF and now my new data is not updated

$post_desc = get_post_meta(get_the_ID(), 'post_desc', true);
$post_id = get_the_ID();

$value = get_post_custom_values(get_the_ID(), $post_desc);
foreach ($value as $values)
    update_post_meta( $post_id, 'post_desc', $post_desc );

post_desc - is new name on ACF

How change existing value to new and keep data ?

I changed the name in ACF and now my new data is not updated

$post_desc = get_post_meta(get_the_ID(), 'post_desc', true);
$post_id = get_the_ID();

$value = get_post_custom_values(get_the_ID(), $post_desc);
foreach ($value as $values)
    update_post_meta( $post_id, 'post_desc', $post_desc );

post_desc - is new name on ACF

Share Improve this question edited Nov 2, 2018 at 8:26 cybmeta 20.7k5 gold badges47 silver badges58 bronze badges asked Nov 2, 2018 at 7:19 gomezgomez 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In order to add a new value and keep the previous values, you need to use add_post_meta() instead of update_post_meta():

$post_id = get_the_ID();
$new_post_desc = 'the new value here';

// add_post_meta() add a new field with the same name
// and keeps previous values on the database
add_post_meta( $post_id, 'post_desc', $new_post_desc );

Then, you can use get_post_custom_values() to get all values for post_desc field, but note that you are using this function incorrectly.

Instead of:

$values = get_post_custom_values(get_the_ID(), $post_desc);

It should be:

$values = get_post_custom_values( 'post_desc', get_the_ID() );

where 'post_desc' (not $post_desc) is the name or the field.

If you want to update one specific value previously set for that field, you can use update_post_meta() passing the previous value you want to update as the fourth parameter:

update_post_meta( $post_id, 'post_desc', 'new_value', 'previous_value' );
Post a comment

comment list (0)

  1. No comments so far