Skip Navigation

[Résolu] Update and create Repeating Fields programmatically

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 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.

Marqué : 

This topic contains 2 réponses, has 2 voix.

Last updated by davidS-53 Il y a 1 an et 11 mois.

Assigned support staff: Beda.

Auteur
Publications
#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.