Home › Forums › General Issues › Trying to automatically assign relationship using updatefield with insert_post › Reply To: Trying to automatically assign relationship using updatefield with insert_post
Okay John, this really helped a lot and pointed me in the right direction. After some testing I managed to get it working, however I do not understand why it works… My process:
1) I implemented exactly as you suggested. However nothing happened..
2) I thought it might be that the day_post
array was not filled correctly so I tried printing that using print_r
Using wp_update_post
:
$my_post = array ();
'ID' => $relatie,
'post_content' => $day_post. '<br>' . $relatie );
wp_update_post($my_post);
3) I started checking whether this was an issue of scope so put the declaration of the day_post
outside of the function, but it did not make a difference
4) Then I checked whether it had something to do with how print_r
worked and made sure it was a returned value:
$results = print_r($day_posts, true);
$results1 = print_r($relatieID, true);
$my_post = array ();
'ID' => $relatie,
'post_content' => $results . '<br>' . $relatie . '<br>' . $results1);
wp_update_post($my_post);
This actually showed that $day_post
was filled correctly and that $relatie
also was the correct input post.
5) only then I saw that it had already worked! However I had no idea why so I started tracing back. Long story short: adding a `wp_update_post’ with an empty array did the trick. I suspect it has something to do with priority of what’s loaded on the page or something… but really have no idea.
Final working code:
$day_posts = array();
foreach ($week as $weekday) {
$PostID = wp_insert_post(array(
'post_title' => $latestpost[0]->post_title . ': ' . $weekday,
'post_type' => 'palaver',
'query_var' => true
));
$day_posts[] = $PostID;
wp_set_object_terms($PostID, $CatTermID, 'gebied');
}
$my_post = array ();
wp_update_post($my_post);
update_field('field_5eea245f36217', $day_posts, $relatie);
If you can shed any light on this John (just because I’m interested in undertanding what’s actually happening), that would be awesome. If you have better things to do I also understand 😉 Muchas Gracias! 🙂
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.