Hi,
I've to update a previous ticket: https://toolset.com/forums/topic/repetable-field/#post-1137433
When I add the room from the backend it is assigned correctly to the accommodation manager as you suggest here: https://toolset.com/forums/topic/problem-in-view-post-author/#post-1146368
Now, I I add a room via frontend using the CRED as describe in the ticket (https://toolset.com/forums/topic/repetable-field/#post-1137433). I've the same problem because the room is not assigned to the property manager.
How can I do?
Hi,
In front-end with Toolset form, you can try with Form action hook cred_save_data, for example, modify the PHP codes I mentioned in your ticket:
https://toolset.com/forums/topic/repetable-field/#post-1137433
As below:
add_action('cred_save_data', 'relate_accommodation_room_func',10,2);
function relate_accommodation_room_func($room_id, $form_data)
{
// if a specific form
$form_ids = array(26474);
if (in_array($form_data['id'], $form_ids))
{
if (isset($_POST['parent-accommodation-post']) && !empty($_POST['parent-accommodation-post']))
{
$accommodation_id = $_POST['parent-accommodation-post'];
// relate those two posts
toolset_connect_posts('acc-room', $accommodation_id, $room_id);
//accommodation post author
$accommodation_post = get_post($accommodation_id);
$accommodation_author_id = $accommodation_post->post_author;
// update room post author
$args = array(
'ID'=> $room_id,
'post_author' => $accommodation_author_id,
);
wp_update_post( $args );
}
}
}
More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
HI Luo, I changed the code as you suggest but nothing is changed.
When I create a room via frontend it is added to the accommodation (I see it in the backend) but it is not correctly set the post author because I not see it in the frontend (view filtered by post author)
Since it is a custom codes problem, please provide your text site credentials, also point out the Toolset form URL and where I can test it in front-end, I need to test and debug it in a live website, thanks
Thanks for the details, same as your previous thread
https://toolset.com/forums/topic/problem-in-view-post-author/#post-1146368
we need to remove the custom action hook before update the post author, I have modified the PHP codes as below:
add_action('cred_save_data', 'relate_accommodation_room_func',999,2);
function relate_accommodation_room_func($room_id, $form_data)
{
// if a specific form
$form_ids = array(26474);
if (in_array($form_data['id'], $form_ids))
{
if (isset($_POST['parent-accommodation-post']) && !empty($_POST['parent-accommodation-post']))
{
$accommodation_id = $_POST['parent-accommodation-post'];
// relate those two posts
toolset_connect_posts('acc-room', $accommodation_id, $room_id);
//accommodation post author
$accommodation_post = get_post($accommodation_id);
$accommodation_author_id = $accommodation_post->post_author;
// update room post author
$args = array(
'ID'=> $room_id,
'post_author' => $accommodation_author_id,
);
remove_action( 'save_post', 'update_room_author', 10 ); // here remove the custom action hook
wp_update_post( $args );
add_action( 'save_post', 'update_room_author', 10, 2 );
}
}
}
Please test again, check if it is fixed, thankx
My issue is resolved now. Thank you!