admin管理员组文章数量:1130349
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
1 Answer
Reset to default 0In 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' );
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
1 Answer
Reset to default 0In 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' );
本文标签: advanced custom fieldsChange post value in WordPress
版权声明:本文标题:advanced custom fields - Change post value in WordPress 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749210471a2333275.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论