Home › Forums › General Issues › Trying to automatically assign relationship using updatefield with insert_post
Hi,
I’m trying to make a day+week post creation combination work. For every post of type week I create I want to create 7 posts of type day. All these day posts should automatically be related to the week post.
So far I have tried it with both update_field and update_post_meta:
$relatie = $latestpost[0]->ID;
$relatieID = get_field('week_palaver');
foreach ($week as $weekday) {
$PostID = wp_insert_post(array(
'post_title' => $latestpost[0]->post_title . ': ' . $weekday,
'post_type' => 'palaver',
'query_var' => true
));
wp_set_object_terms($PostID, $CatTermID, 'gebied');
1) update_field('field_5eea245f36217', array($PostID) , $relatie);
2) update_field($relatieID, $PostID , $relatie);
3) update_post_meta($relatie, 'field_5eea245f36217', $PostID);
}
The $relatie variable represents the ID of the week post
The $relatieID variable represents the field which should be updated (as you can see I have tried it with the field key as well)
The $PostID variable is the new post ID returned by wp_insert_post on success
Based on a different thread I have tried update_post_meta, which has not yielded any results either thus far.
Could anyone point me in the right direction? Greatly appreciated! 🙂
The value of the ralationship field needs to be an array or all relationships, you can’t update them one at a time. Collect them in your loop and update them when the loop is completed.
$relatie = $latestpost[0]->ID;
$relatieID = get_field('week_palaver');
$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');
}
update_field('field_5eea245f36217', $day_posts , $relatie);
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! 🙂
Going to be honest, I don’t now why you would need to do update post with an empty array to get it work, it should not be needed. So I’m as confused as you are.
Hmmm that’s too bad.
Final update: an empty array did not completely work. Somehow leaving it empty made it impossible to actually publish the post (status remained “draft”) so I filled it with some dummy text and now it does what it should do. This issue must be somewhere deep in the WP code.
Really not happy about the fact that I do not understand why it works because I have the feeling it will bite me in the ass at some point, but at least it does work.
Thanks for the help!
The topic ‘Trying to automatically assign relationship using updatefield with insert_post’ is closed to new replies.
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.