Skip Navigation

[Gelöst] Assign repeatable field to author via frontend

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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

This support ticket is created vor 5 Jahre, 5 Monate. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 Antworten, has 2 Stimmen.

Last updated by domenicoS vor 5 Jahre, 5 Monate.

Assisted by: Luo Yang.

Author
Artikel
#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

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

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

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!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.