Skip Navigation

[Resolved] PHP add repeatable fieds

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 6 replies, has 2 voices.

Last updated by Waqar 2 months, 1 week ago.

Assisted by: Waqar.

Author
Posts
#2683457

How can I code to insert/update/delete repeatable field and associate to specific custom post?

#2683522

Waqar
Supporter

Languages: English (English )

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

Hi,

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

For repeatable fields where multiple instances of values are allowed, you can use the 'add_post_meta' function, with the 'unique' parameter set to 'true':
https://developer.wordpress.org/reference/functions/add_post_meta/

This way each new value will be saved as a new entry and won't overwrite the existing custom field values.

regards,
Waqar

#2683525
Screenshot 2024-02-15 at 1.58.48 PM.png
Screenshot 2024-02-15 at 1.56.55 PM.png

Dear Waqar,

Could you please explain how I can get the correct $meta_key, $meta_value if my repeatable group slug is ecard-contact-group and field slug is contact-type and how do I update the title of each rfg ? Giving me the sample code is appreciated.

#2683542

Waqar
Supporter

Languages: English (English )

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

Thanks for writing back.

I thought you were referring to the repeatable field, but from the screenshot, you're clearly using the repeatable field group.

The fields in the repeatable field group are stored with a separate hidden post type connected through a one-to-many post relationship with the parent post type.

Can you please share the temporary admin login details of the website, along with the exact place where you're planning to use this code? I'll be in a better to suggest a code example, accordingly.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2683544

Here is my script

$post_id = 9;
printf("post_id: %s<br/>", $post_id);


// create repeatable group
$new_post_id = wp_insert_post( array(
    'post_title' => "pending rfg for $post_id",
    'post_status' => 'publish',
    ));
printf("new_post_id: %s<br/>", $new_post_id);    

wp_update_post(array(
    'ID' => $new_post_id,
    'post_name' => $new_post_id
    ));

$result = add_post_meta($new_post_id, 'wpcf-contact-type', 'mobile');
if ($result) {
    printf("wpcf-contact-type updated %s<br/>", $result);
}
$result = add_post_meta($new_post_id, 'wpcf-content', '98765432');
if ($result)  {
    printf("wpcf-content updated %s<br/>", $result);
}

// create relationship between new repeatable post and ecard post
toolset_connect_posts('ecard-contact-group', $post_id, $new_post_id);
#2683574

I rewrite my code as below and work

$post_id = 9;
printf("post_id: %s<br/>", $post_id);


// create repeatable group
$new_post_id = wp_insert_post( 
    array(
        'post_type' => 'ecard-contact-group',
        'post_title' => "pending rfg for $post_id",
        'post_status' => 'publish',
        'meta_input' => array(
            'wpcf-contact-type' => 'mobile',
            'wpcf-content' => rand(10000000, 99999999),
        )
    )
);
printf("new_post_id: %s<br/>", $new_post_id);    

wp_update_post(array(
    'ID' => $new_post_id,
    'post_title' => 'Mobile (Testing) - ' . rand(1000,9999),
    'post_name' => $new_post_id
    ));

toolset_connect_posts('ecard-contact-group', $post_id, $new_post_id);

What is the problem? The first sample I insert post and add post_meta one by one and then build the relationship, the second is inserting the post all in one including the post_meta and then build the relationship, why the first one not work ?

I may want to check whether the rfg already existed before I do the add_post_meta(), can do?

#2683766

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing this update and glad that you were able to sort it out.

In the first instance of the code, I don't see the post type being specified at the time of new post creation.
( post_type' => 'ecard-contact-group', )

You can fix it and then test the same code again.

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