Hi team, I'm using Formidable forms to create and edit two different types of posts, "Studios" and "Artists". I need help passing the post_id of the Studio to a field (Studio Post ID) in the Artist post. Here's what I've got set up and working so far:
My problem is that the hook frm_after_create_entry seems to run after the API action, so when the API action sends Studio entry data to the Create Artist form the Studio Post ID field is blank.
I've tried using the frm_api_action_options filter to make the API action happen after the Create Post action as per this example but it hasn't worked either. I'm wondering if changing the priority number will help? (I don't have a clear understanding about how priority numbers work in WordPress actions.)
The goal of all this is to populate an ACF Relationship field on the Artist post that links the Artist to the correct Studio post.
Anyone got any other ideas on how to approach this?
As is often the case, I think I've solved this one for myself! The solution I found was to abandon using the Formidable API method of creating a submission in my second form and just use the frm_after_create_entry hook instead.
I started with this code example and then modified it thus:
add_action('frm_after_create_entry', 'create_entry_after_submission', 60, 2);
function create_entry_after_submission($entry_id, $form_id){
if ( $form_id != 2 ) { //ID of Form A
return;
}
//Get the initial form entry and find the post_id of the post created by this entry.
$entry = FrmEntry::getOne( $entry_id );
if ( ! $entry->post_id ) {
return;
}
$form2 = '4'; //ID of Form B
//Get user
if (!isset($_POST['frm_user_id'])){
return;
}
$user = $_POST['frm_user_id'];
//get data
if (!isset($_POST['item_meta'][84])){ //84 is the field ID of the Form A value
return;
}
$studio_post_id = $entry->post_id; //assign the post_id to this variable
$artist_name = $_POST['item_meta'][84];
$artist_description = $_POST['item_meta'][229];
//create entry
FrmEntry::create(array(
'form_id' => $form2,
'item_key' => $user . $entry_id,
'frm_user_id' => $user,
'item_name' => $artist_name,
'item_meta' => array(
171 => $user,
225 => $studio_post_id,//Put the post_id into the correct field of the new form entry
98 => $artist_name,
121 => $artist_description,
),
));
}
I'm a novice coder so I've just hacked this together until it worked. If any boffins out there can see any issues or any ways to improve it I'd be much obliged!
Please login or Register to submit your answer