Skip Navigation

[Resolved] Adding values to a repeatable group using update_post_meta

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.

Our next available supporter will start replying to tickets in about 4.93 hours from now. Thank you for your understanding.

Author
Posts
#2183967

Tell us what you are trying to do?
I have a function which runs after a certain form is submitted and this creates a new entry for a custom post type created using Toolset. This function uses the update_post_meta function to set the value of my custom fields and this works fine, however within my custom post type I have a repeatable group (notes) with one field "case-note-date" and another "case-note" and I'd, ideally, like to be able to use my function to enter values for these fields too, such as today's date for the case-note-date field and text along the lines of "Case created by x user" for the case-note field?

Is there any documentation that you are following?
I looked at other threads about programmatically inserting values for repeating fields but am not sure if the process varies for repeatable field *groups* as opposed to simply repeatable fields.

#2184425

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

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 )

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 Pricing Plan' (slug: product-pricing-plan).

When you'll insert a new post in the "Product" post type, you'll also need to insert a new post in the hidden post type "product-pricing-plan", to fill the custom field values in the repeatable field group.

After that, you can connect the IDs of the newly added "Product" (parent) and "Product Pricing Plan" (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 Pricing Plan" post and not the "Product" post.

Here is an example code snippet, that adds a test "Product" post and then adds a new repeating field group post "product-pricing-plan", joins them in the relationship, and updates the custom field value in the field "field-slug" in the repeatable field group.


// 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 );
 
    }
}

You can include a similar code in your custom function that executes on form submission. Of course, you won't need the first part of the code to create the parent post, as the form will be creating the parent post for you.

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

regards,
Waqar

#2184595

Thanks Waqar, this was exactly the guidance I was needing and very helpful. Good to know how the repeating fields work under the hood as well.

Kind regards

Simon