Skip Navigation

[Resolved] How to add repeatable field values via code?

This support ticket is created 5 years, 5 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
- 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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 5 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#1282771

Hi,

I have a repeatble field called 'Product Features' with a text input field 'feature name'. I am able to create a custom post via code and add other fields but don't know how to add a repeatble field.

I am using update_post_meta function to add fields data.

Kindly help me with this.

#1283983

Hi there,

Thank you for contacting us.

Nigel will be away on vacations until next week and I'll be helping you with this ticket.

The process to update the custom field value will be different if the field is a "repeating field" ( ref: https://toolset.com/documentation/user-guides/repeating-fields/ ) or is a part of a "repeating field group" ( ref: https://toolset.com/documentation/getting-started-with-toolset/creating-and-displaying-repeatable-field-groups/ ).

A. Case - Repeating field:

As explained in the documentation ( ref: https://toolset.com/documentation/user-guides/repeating-fields/#How%20Types%20Stores%20Repeater%20Fields ), Types stores repeating fields as individual post-meta in the database. This means that when you add another field, a new post-meta is created with the same key, but a different value.

If you need to add multiple values/entries for a repeating field, you can use "add_post_meta" function, instead of "update_post_meta"
( ref: https://codex.wordpress.org/Function_Reference/add_post_meta )

The "update_post_meta" function updates the value of the existing custom field entry and only adds a new one if it doesn't already exist.

B. Case - Field in Repeating Field Group:

When a repeating field group is created, a new hidden post type is also created in the backend, with the same slug as that repeating field group.
( example screenshot: hidden link )

Also, a hidden relationship with the same slug is also created between this new post type (child) and the parent post type, for which this repeating field group was added.

Any fields which are added as part of that repeating field group, are saved as custom meta field entries against this new custom post type (child) and not the parent one.

For example, suppose you have a "Product" post type, for which you have a repeating field group called 'Product Features' (slug: product-features).

When you'll insert a new post in "Product" post type using the code, you'll also need to insert a new post in the hidden post type "product-features".

After that, you can connect the IDs of the newly added "Product" (parent) and "Product Features" (child) into a relationship, using the "toolset_connect_posts" function.
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts )

As for the custom fields, you can add or update them using the ID of that new "Product Feature" post and not the "Product" post.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1284027
repeatfield.png

Hi,

As per the attached image, is it possible to share a sample php code which will help me understand better. Thanks in advance.

#1284501

Hi,

Here is a sample code and please update the slug of the post types and the custom field(s), as used on your website.


// adding "Product" post
$product_post = array(
	'post_title'    => "this is a product post",
	'post_status'   => 'publish',
	'post_author'   => 1,
	'post_type' => 'product'
);

$product_post_id = wp_insert_post( $product_post );


// if "Product" post has been added successfully, add a new "Product pricing plan" post (RFG)
if($product_post_id) {

	$ppp_post = array(
		'post_title'    => "this is a product pricing plan post",
		'post_status'   => 'publish',
		'post_author'   => 1,
		'post_type' => 'product-pricing-plan'
	);
	
	$ppp_post_id = wp_insert_post( $ppp_post );
	
	// if "Product pricing plan" post has been added successfully, update custom field value and add a relationship connection
	if($ppp_post_id) {

		// add or update custom field value in the new "Product pricing plan" post
		update_post_meta( $ppp_post_id, 'wpcf-field-slug', 'This is the value of the field');

		// join "Product" and "Product pricing plan" posts through a relationship
		toolset_connect_posts( 'product-pricing-plan', $product_post_id, $ppp_post_id );

	}
}

For more personalized assistance around custom code, you can hire a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar