Home › Forums › Backend Issues (wp-admin) › Saving values to another post on post save
I have the custom post type ‘machines’.
Machines has two custom fields of the type post object: “company” and “seller”.
On saving the machine post type, I want to update the company post (which has a ‘sellers’ repeater and a ‘machines’ repeater) with
the ‘seller’ id selected for ‘machine’ and the ‘machine’ id itself.
Also I want to update the ‘seller’ post’s ‘company’ custom field with the ‘company’ post type id that has been selected for the machine post type.
I have written the following function to accomplish this:
add_action('save_post_machines', 'associate_machine', 10, 3);
function associate_machine($post_ID){
//Add seller and machine to company page
$company = get_field('field_60f1861f1b262', $post_ID); //company ID
$seller = get_field('field_60f186361b263', $post_ID); //seller ID
$whichs = ['machine' , 'seller'];
foreach($whichs as $which):
$post_ID = $which == 'machine' ? $post_ID : $seller;
$repeaterValues = [];
if(have_rows($which.'s', $company)): //Machines or sellers repeater
while(have_rows($which.'s', $company)):
the_row();
$repeaterValues[] = get_sub_field($which, $company);
endwhile;
else:
add_row($which.'s', array($which => $post_ID), $company);
endif;
//if machine or seller ID is already inserted, simply stop here
if(in_array($post_ID, $repeaterValues)):
elseif(!in_array($post_ID, $repeaterValues) && !empty($repeaterValues)): // if not, add it
add_row($which.'s', array($which => $post_ID), $company);
endif;
endforeach;
//Add company to seller:
update_field('company', $company, $seller);
But instead of having the seller id in the sellers repeater, I only get an empty row.
Additionally, and this is strange: I was testing this on my staging site, and it was working. Then uploading the same code to live, it is not. (I made a compare of all theme files of live and staging and matched them up 100%, and still.
I also checked if there were any plugins that were not updated on the staging site, still no change.
So I have worked on this for a while and can’t seem to find what I am missing.
Any insights would be appreciated.
The problem was that the custom field data of the machine post was probably not saved yet.
using add_action('save_post_machines', 'associate_machine', 10, 3);
using
add_action('acf/save_post', 'associate_machine', 11);
instead of post save
, resolved the problem.
What I had trouble finding before is that the priority should apparently be > 10.
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.