Home › Forums › General Issues › Problems with saving values to ACF-Fields when creating a new post
Hi there,
I want to update the $_POST[‘acf’]-Array with specific values for some field_keys. I use ACF with another plugin called “Event Organiser” which register the custom-post-type “event”. I have several ACF-Fields
When the user creates or edits an event my function should update the field for the thumbnails referring to the chosen district. Also it should update the fields for deleted and postponed.
I’ve two custom functions to get the choices of the select field “districts” and to get the field_keys for the thumbnail-fields for the different districts and the fields for deleted and postponed.
Here my code:
function update_image_fields( $post_id ){
$post_type = get_post_type( $post_id );
if (isset ($_POST['acf']['field_5ecf8ef280a84'])){
$new_district = strtolower($_POST['acf']['field_5ecf8ef280a84']);
} else {
$new_district = "bayreuth";
}
$image_urls = array (
'bamberg' => wp_get_attachment_image_url(244),
'bayreuth' => wp_get_attachment_image_url(244),
'coburg' => wp_get_attachment_image_url(244),
'forchheim' => wp_get_attachment_image_url(244),
'hof' => wp_get_attachment_image_url(244),
'kronach' => wp_get_attachment_image_url(244),
'kulmbach' => wp_get_attachment_image_url(244),
'lichtenfels' => wp_get_attachment_image_url(244),
'wunsiedel' => wp_get_attachment_image_url(244),
'postponed' => wp_get_attachment_image_url(476),
'deleted' => wp_get_attachment_image_url(481),
);
$acf_thumbs = get_district_choices ( );
array_push ($acf_thumbs, "deleted", "postponed");
if ('event' == $post_type){
//update all thumbnail_fields
$test_meta_key = "_thumbnail_" . $new_district;
$test = acf_get_all_field_key ( $test_meta_key, $post_id );
foreach ($acf_thumbs as $acf_thumb) {
$meta_key = "_thumbnail_" . $acf_thumb;
// if there is a new event
if (false === $test) {
$args = array (
'numberposts' => 1,
'post_type' => 'event',
'post_status' => 'publish',
'fields' => 'ids',
);
$new_post_id = get_posts($args)[0];
$field_keys[$acf_thumb] = acf_get_all_field_key ( $meta_key, $post_id = $new_post_id );
}
// for an update of an event
else {
$field_keys[$acf_thumb] = acf_get_all_field_key ( $meta_key, $post_id );
}
}
foreach ($field_keys as $field_key => $field_value) {
switch ($field_key) {
case "deleted" :
$fk_deleted = $field_keys['deleted'];
$_POST['acf']["$fk_deleted"] = $image_urls['deleted'];
break;
case "postponed" :
$fk_postponed = $field_keys['postponed'];
$_POST['acf']["$fk_postponed"] = $image_urls['postponed'];
break;
case $new_district :
$fk_new_district = $field_keys["$new_district"];
$_POST['acf']["$fk_new_district"] = $image_urls["$new_district"];
break;
default :
$fk_others = $field_keys["$field_key"];
$_POST['acf']["$fk_others"] = "";
break;
}
}
}
}
add_action( 'acf/save_post', 'update_image_fields', 5);
For updating an event, it do what it should. But when I am creating a new event nothing is updating. I think it’s overwritten by any build-in-function or by ACF when WordPress creates a new post. Can you please give me hint what I’m doing incorrect or what I can do otherwise? Is “acf/save_post” the correct action to hook for a new post/ event?
Thank you and all the best!
What is this function doing?
acf_get_all_field_key()
This is not a function I can find in ACF
Also,
$field_keys[$acf_thumb] = acf_get_all_field_key ( $meta_key, $post_id = $new_post_id );
sets the index of $acf_thumb, but here you are not using this index
foreach ($field_keys as $field_key => $field_value) {
I would assume by looking at your code that this should be
foreach ($field_keys[$acf_thumb] as $field_key => $field_value) {
Hi John! Thank you for your response! It was the function “acf_get_all_field_keys”. It’s a custom function which gets the field-keys (field_123456) from the database. Here is the code:
function acf_get_all_field_key( $meta_key ) {
global $wpdb;
$acf_fields = $wpdb->get_results( $wpdb->prepare( "
SELECT 'meta_value'
FROM '$wpdb->postmeta'
WHERE 'post_id' = 3077
AND 'meta_key'
LIKE %s",
$meta_key
)
);
// get all fields with that name.
switch ( count( $acf_fields ) ) {
case 0: // no such field
return false;
case 1: // just one result.
return $acf_fields[0]->meta_value;
default:
return array ($acf_fields);
}
}
I changed the post-id for this function to a fix ID of a published event, so it’s always returning values.
I founded this function here: https://gist.github.com/mcguffin/81509c36a4a28d9c682e
Thank you for your help! This topic is solved.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.