[Resolved] Assign repeatable field to author via frontend

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This thread is resolved. Here is a description of the problem and solution.

Problem:

Change repeatable field group post's author to the current post when submit post form in front-end.

Solution:

It needs custom codes, see details here:

https://toolset.com/forums/topic/assign-repeatable-field-to-author-via-frontend/#post-1147051

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by domenicoS 1 month ago.

Assigned support staff: Luo Yang.

Author
Posts
#1146647

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?

#1146980

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

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

#1147013

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)

#1147015

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

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

#1147051

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

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

#1147750

My issue is resolved now. Thank you!