Skip Navigation

[Resolved] Trying to auto fill the title of repeatable group title with field from parent

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

Problem:

The issue here is that the user is creating their RFG on the frontend but wanted the title of the RFG to be auto generated.

Solution:

The user had this code below.

add_action('cred_save_data', 'delegate_title_save_data_action', 10, 2);
function delegate_title_save_data_action( $post_id, $form_data ){
// if a specific form
if ($form_data['id']==35913) {
if(!empty($_POST['wpcf-idx_chantier'])){
$idx_chantier = $_POST['cred_post_parent get="wpcf-idx_chantier"'];
}

$title = $idx_chantier;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

In this user's case they needed to have replace

$idx_chantier = $_POST['cred_post_parent get="wpcf-idx_chantier"'];

with the proper call for the post parent, which would be
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

This support ticket is created 5 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by mikeB-14 5 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1274639

I have added this under the custom code section in toolset

This is my code
But the title field is blank

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

// fill delegate group repeatable title with idx_chantier from parent

add_action('cred_save_data', 'delegate_title_save_data_action', 10, 2);
function delegate_title_save_data_action( $post_id, $form_data ){
// if a specific form
if ($form_data['id']==35913) {
if(!empty($_POST['wpcf-idx_chantier'])){
$idx_chantier = $_POST['cred_post_parent get="wpcf-idx_chantier"'];
}

$title = $idx_chantier;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

#1274751

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

Thank you for contacting our support forum.

Are you making reference to repeatable field groups that you are populating on the backend ?

Please let me know.

Thanks,
Shane

#1275517

So the idx_chantier is already a populated field held in the parent (planner event) so I want the idx_chantier value to be assigned to the post title of repeatable field group.

In fact, I don't really need a post title for the repeatable field group., so can it be taken out of the cred form.
What I have seen in the past is if you don't have a value for the post title, you end up with these auto draft fields.
Tony

#1275789

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Tony,

But this doesn't answer my question, Is this on the frontend or the backend.

Are you creating the Repeatable field groups through the frontend on a CRED form ?

I need a little more context to help better.

Thanks,
Shane

#1275853

Hi Shane

Sorry about that .
I am adding the Repeatable field groups via the frontend using a cred form

Tony

#1275869

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

I think the issue is here.

$idx_chantier = $_POST['cred_post_parent get="wpcf-idx_chantier"'];

You should use this function below to get the parent.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

If you use this function you should be able to get the parent id.

Thanks,
Shane

#1276789

My issue is resolved now. Thank you!