I use a hook edit_post_(post_type)
. After updating a record, I get two new records at once when I only need one. What am I doing wrong?
add_action('edit_post_applications', 'add_new_university');
function add_new_university($id)
{
$post_field = get_fields($id);
return wp_insert_post(array(
'post_type' => 'institution',
'post_title' => get_post($id)->post_title,
'post_status' => 'publish',
'meta_input' => array(
'full_name' => $post_field['full_name'],
'short_name' => $post_field['short_name']
),
));
}
I tried different hooks but couldn’t fix this problem
3
Answers
Thank you. This helped:
The edit_post hook (and similar hooks) can be triggered multiple times during a single post save operation. This can happen due to various reasons like revisions, autosaves, or other plugins/themes interacting with the save process.
Added checking for autosave, revision, update and checking if already processed (by using post metadata).
Another solution to the problem is to add a check