Skip Navigation

[Resolved] Update and create Repeating Fields programmatically

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

Problem:
How can I update or create Repeating fields using WordPress's API to manipulate meta fields (get_post_meta/update_post_metae/add_post_meta)?

Solution:
Toolset doesn't offer a specific API for this, and our Support Guidelines don't allow direct support on such custom code, we can, however, give an example that will help understand how repeating fields (not repeatable field groups, but repeating fields) can be retrieved and updated.

With repeating fields we have meta key, meta value, and the position of each of the values, to consider in our code.

To update, or post such fields, you need not only to pass an array of those values to the field but also the position of each item in the repeating field.

Below link holds a full example code, that can be used as a starting point for further customization.

Relevant Documentation:
https://kb.onthegosystems.com/code-snippet/get-and-post-repeating-fields-correctly-persisting-the-order/

This support ticket is created 4 years, 7 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by davidS-53 4 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#1319869

I have a repeating text field called all-safety-terms, which belongs to a post type called boats. I've written a function to sort through all the values of the field, clear out blank ones, and sort them alphabetically. For example, take this:

[0] => 
[1] => NUC Lights
[2] => Smoke Alarm
[3] => Binoculars

And sort/clean it to:

[0] => Binoculars
[1] => NUC Lights
[2] => Smoke Alarm

Here's my function- it all works fine (the array is in the right order and is cleaned), up until updating the array back to the field. I assume I need to use

update_post_meta()

for this? I can't see anything in the docs that relates to this. Here's my current function:

function sf_clear_blank_terms_order_rest( $post_id ) {

	// Post Types to include
	$post_args = array(
		'posts_per_page' => -1,
		'post_type' => array(
			'boat'
		),
	);

	// The Query
	$post_query = new WP_Query( $post_args );
	$posts = $post_query->posts;

	foreach( $posts as $post ) {

		$boat_safety_terms = get_post_meta( $post->ID, 'wpcf-all-safety-terms', false );
		$boat_safety_terms_cleaned = array_filter( $boat_safety_terms );
		sort( $boat_safety_terms_cleaned );

		error_log( print_r( $boat_safety_terms_cleaned, TRUE ) );

		update_post_meta( $post->ID, 'wpcf-all-safety-terms', $boat_safety_terms_cleaned );

	}

}
add_action('wp_footer', 'sf_clear_blank_terms_order_rest', 10, 1);

Any ideas what I might be doing wrong? Cheers!

#1320039

Our Support Guidelines don't allow direct support on such custom code, I can, however, give an example that will help you understand how repeating fields (not repeatable field groups, but repeating fields) can be retrieved and updated:
hidden link

As you can see there, with repeating fields we have meta key, meta value, and the position of each of the values.

To update, or post such fields, you need not only to pass an array of those values to the field but also the position.

I hope the snippet there helps achieve the goal, please don't hesitate to ask for feedback if not.

#1323739

Thanks Beda, that helps a lot.

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