Skip Navigation

[Resolved] Repeatable Field Group cannot be displayed when created programmatically

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Minesh 1 year, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2654301
Screenshot 2023-10-19 at 00.24.20.png
Screenshot 2023-10-19 at 00.24.08.png

Hi,
I have a repeatable field group that I display on the website. Here's an example: hidden link (Look for the available dates). Everything works great if I create a post from the admin page.

Now I have created a form where users can create their own posts. I did some custom coding to be able to support the repeatable field group. Here's the code that creates them:

add_action('cred_save_data', 'hvf_add_available_dates_to_company',10,2);

function hvf_add_available_dates_to_company($post_id, $form_data) {
if ($form_data['id'] == 23899) {
$availableDates = $_POST['available-date'];

foreach ($availableDates as $availableDate) {
// the data comes in the following format: "d/m/Y to d/m/Y"
[$startDate, $endDate] = explode(' to ', $availableDate);
$startDateTimestamp = DateTime::createFromFormat('!d/m/Y+', $startDate)->getTimestamp();
$endDateTimestamp = DateTime::createFromFormat('!d/m/Y+', $endDate)->getTimestamp();

$args = array(
'post_title' => 'Available Date ' . $availableDate . ' for '. $post_id,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'available-dates',
'meta_input' => array(
'wpcf-start-date' => $startDateTimestamp,
'wpcf-end-date' => $endDateTimestamp,
)
);

// create the available dates
$availableDateId = wp_insert_post( $args );
toolset_connect_posts( 'available-dates', $post_id, $availableDateId );
}
}
}

The code runs without a problem. When I look at the post on the admin page, I see that the association is created without any problem. But when I look at the page where I display the data, I see that it wouldn't be displayed ("coming soon" is the message I display when no item is found).

I wonder if there's a problem the way I create the association or is there a cache problem going on. How can I understand what is wrong with the code or the view?

#2654381

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I see the repeating field items seems to be added correctly using the code and it does displayed at the backend edit page.

I need to know how exactly you are trying to display the repeating field group?

Here is the Doc that shows information about how you can display the repeating field group:
- https://toolset.com/course-lesson/creating-and-displaying-repeatable-field-groups/

Please let me know if you already followed the above doc and you require further assistance.

#2654405
Screenshot 2023-10-19 at 09.04.05.png

Hi Minesh, thank you for looking into this.

I did follow that page when I first set that up. I think it is correct because if I add the RFG from the admin page everything works without a problem. That's why I'm confused.

The view is set up to display the RFG. I have a "Fields and Text" block in it. This is what I have in that block:
<p>[types field='start-date' style='text' format='d/m/Y'][/types] - [types field='end-date' style='text' format='d/m/Y'][/types]</p>

#2654455

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please tell me using what form you are creating repeating field group as well as share admin access details and problem URL where you are displaying the repeating field group.

Let me see whats going wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2655283

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should now try to create a new event from frontend and check.

The reason why it was not working because you set your view that displays the repeating field group 'available-dates' items is set to order by 'toolset-post-sortorder' and when you create the repeating field group items using PHP custom code:

function hvf_add_venue_and_available_dates_to_company($post_id, $form_data)
{
	
	
    // if it is the add camp form
    if ($form_data['id'] == 23899) {	
		
		
		$availableDates = $_POST['available-date'];
		$venueList = $_POST['venueList'];
		
		foreach ($availableDates as $availableDate) {
			[$startDate, $endDate] = explode(' to ', $availableDate);
			$startDateTimestamp = DateTime::createFromFormat('!d/m/Y+', $startDate)->getTimestamp();
			$endDateTimestamp = DateTime::createFromFormat('!d/m/Y+', $endDate)->getTimestamp();
			
			$args = array(
				'post_title'     => 'Available Date ' . $availableDate . ' for '. $post_id,
				'post_status'    => 'publish',
				'post_author'    => get_current_user_id(),
				'post_type'      => 'available-dates',
				'meta_input'     => array(
					'wpcf-start-date' => $startDateTimestamp,
					'wpcf-end-date' => $endDateTimestamp,
				)
			);

			// create the available dates
			$availableDateId = wp_insert_post( $args ); 
			toolset_connect_posts( 'available-dates', $post_id, $availableDateId ); 
		}
		
		foreach ($venueList as $venueId ){
			toolset_connect_posts( 'venue-event', $venueId, $post_id ); 
		}
    }
	
}
</ code>

There is no toolset-post-sortorder set for repeating field group items. 


To set the toolset-post-sortorder field value - I've added the following hook to your code snippet:
=> <em><u>hidden link</u></em>
[php]
add_action('cred_submit_complete', 'func_set_repeating_field_group_order_manually',99,2);
function func_set_repeating_field_group_order_manually($post_id, $form_data) {
    // if a specific form
    if ($form_data['id']==23899) {
    	//Set your RFG slug
       	$rfg_slug = 'available-dates';

    	//Get the parent post of the currently added RFG:
    	$parent_post = $post_id;

    	//Now get all potential RFGs of that parent
    	$all_rfg = toolset_get_related_posts($parent_post, $rfg_slug, 'parent',999,0,array(),'post_id','child');
		
				
        //If we have any results
    	if (!empty($all_rfg) and is_array($all_rfg)) {
            foreach($all_rfg as $k=>$v):
				$order_item =  1;
    			update_post_meta($v, 'toolset-post-sortorder', $order_item);
			endforeach;
			
    	}   
}
}

I can see its working with the test event I've created:
- hidden link

#2656005

Thank you Minesh, this worked perfectly