Skip Navigation

[Resolved] Assign parent post title to child post title using CRED

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

Problem: I have a CRED form that allows Users to create child posts using CRED. They can select a parent post in the CRED form. I would like to use the parent post title as the child post title.

Solution:
Automate this with PHP using the cred_save_data hook:

add_action('cred_save_data', 'copy_parent_title_to_child',10,2);
function copy_parent_title_to_child($post_id, $form_data)
{
  // if a specific form
  if ($form_data['id']=='12345')
  {
    if (isset($_POST['_wpcf_belongs_parentslug_id']))
    {
      $my_post = array(
      'ID' => $post_id,
      'post_title' => get_the_title($_POST['_wpcf_belongs_parentslug_id'])
      );
     wp_update_post( $my_post );
    }
  }
}

Replace the form ID and custom post type slug, and remove the post title field from the child post CRED form.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Joan 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#597779

I have a cred form to create post types items where I choose the parent post type item trough a select control and when I click to the submit button it is created a child post item. I want that this selected parent title was the post title of the child post type item created.

I try to add to assign the post-title trough a cred field that it would be hidden.

I get a similar issue in: https://toolset.com/forums/topic/auto-fill-child-post-title-with-parent-title/

What I was not able to get the title of the parent post. Instead I get the title of the page when I try to get it. This is the code of the cred form:

[credform class='cred-form cred-keep-original']

[cred_field field='form_messages' value='' class='alert alert-warning']

<div class="form-group">
<label>Parent title</label>
[cred_field field='_wpcf_belongs_socicdp_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>
<label>Child title</label>
[cred_field field="post_title" post="child slug" value="[wpv-post-title id="$parent"]" urlparam=""]

I try to use [cred_post_parent get='title' post_type='child slug'] instead of [wpv-post-title] but I get some problem with the syntax of the nested shortcode. But if I insert just the code:

[cred_post_parent get='title']

I get the title of the page instead of the post title of the parent.

Is there a way to do it? If is possible trough shortcodes it was perfect, elsewhere by code.

Thanks.

#597857

There's not a way to do this with shortcodes, but you could use the cred_save_data hook to automate it.
- Edit your child CRED form. Delete the Post Title input field.
- Add custom code to your functions.php file that will set the child post title to match the parent post title:

add_action('cred_save_data', 'copy_parent_title_to_child',10,2);
function copy_parent_title_to_child($post_id, $form_data)
{
  // if a specific form
  if ($form_data['id']=='12345')
  {
    if (isset($_POST['_wpcf_belongs_parentslug_id']))
    {
      $my_post = array(
      'ID' => $post_id,
      'post_title' => get_the_title($_POST['_wpcf_belongs_parentslug_id'])
      );
     wp_update_post( $my_post );
    }
  }
}

Change '12345' to match the numeric ID of your CRED form, then change 'parentslug' in both places to match the slug of your parent post type. For example, if your parent post type slug is "something-great" then your code should be:

$_POST['_wpcf_belongs_something-great_id']
#598325

Perfect!

Thanks.