Skip Navigation

[Resolved] Populate Child Post Title with Parent Post Title

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

Problem:
Client is using the cred_save_data hook to dynamically generate a child post title based upon the title of the parent post, how should they retrieve the correct parent post ID?

Solution:
This changed with Types 3, which no longer stores the parent post ID on the child post.

You would get the parent post ID using the toolset_get_related_post function of the new API.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

This support ticket is created 6 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 9 replies, has 2 voices.

Last updated by julieP 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#908678

I have forms where I don't want the user to be able to change the parent post when submitting a child post so the selector field is hidden. To avoid child post title that look like this: CRED Auto Draft 2d64c7b961563497c88ffc9e24fe0019, I use this hook to update the post title on the child post with the post title of the parent post:-

add_action('cred_save_data', 'save_data_form_2911',10,2);
function save_data_form_2911($post_id, $form_data){

    if ($form_data['id']==2911) {
    $parent_id = get_post_meta($post_id, '_wpcf_belongs_parentpost_id', true);
    $parent_title = get_the_title($parent_id);
    $childpost_title = array(
      	'ID'           => $post_id,
        'post_title'   => $parent_title,
  	);
   wp_update_post( $childpost_title );
}
}

This works for CRED forms but how do I implement this in the new world of Toolset forms?

#908728

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Julie

You mention two seemingly unrelated things in your question, or is it 2 questions?

* you hide the parent selector field so that the parent cannot be changed
- as before you would do this with CSS. Where you insert the relationship field give the wrapper div a class name you can target as required

* you use the cred_save_data hook to give the child post a title
- presumably this is required because you hide the title field in the form and don't want users to enter a title

The principles of this are the same before and after, but the specifics depends on your particular case.

Firstly, if you have an existing site and have updated to Types 3.0 but have not run the relationships migration, this will work exactly as before. If you *have* run the migration, it will also work as before, assuming you make no other changes to your forms, because there is a backwards-compatibility layer to ensure it does.

It becomes an issue when you are creating new forms that include relationship fields, for which you would need to use the new relationships API to get the parent post, specifically toolset_get_related_post, in place of using get_post_meta.

See https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

If you need some more help doing that let me know.

#908804

Hi Nigel

I'm doing a bit of both! It doesn't seem to get any easier to find what I'm looking for...

I'll have a look at the link you've provided and hopefully all will become clear. Right now I'm struggling to understand how we go about things now. I understand which relationship type/s I need to use and when but am unclear how we go about building things now. For the purposes of my question, please assume we're talking about a brand new install (not an existing site that's been migrated). Can you confirm whether the following statements are correct or not please?

1. If no relationship has been set up between post types, post forms should be created via Toolset > Post Forms > Create
2. If a relationship has been set up between post types, a Relationship form should be created

Thanks

#908812

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Julie

Not quite. I don't think we have communicated this very well.

If you want to set-up post relationships with a front-end form, for many-to-many relationships you can only do that with a "Relationship Form".

For one-to-many relationships you can do it the same as before, including a parent selector in a child post form, although the mark-up used for the parent field has changed from before (but you won't need to worry about that if you auto-generate the form).

#908815

I don't currently have any many-to-many relationships. I've noticed the new mark-up for the parent field and been creating forms as I always have done but either something's changed and I'm not doing it right anymore or there's a bug.

Thanks for your help

#908853

Hi again

I've had a look at the link you provided but I'm struggling to translate my old piece of code as required. Can I take you up on your offer of further assistance please?

I've changed my code to this:-

add_action('cred_save_data', 'save_data_form_3319',10,2);
function save_data_form_3319($post_id, $form_data){
 
    if ($form_data['id']==3319) {
    $parent_id = toolset_get_related_post($post_id, array('parentpostslug', 'childpostslug') );
    $parent_title = get_the_title($parent_id);
    $childpost_title = array(
        'ID'           => $post_id,
        'post_title'   => $parent_title,
    );
   wp_update_post( $childpost_title );
}
}

but it's updating with the title of my child post form. I'm so stressed out right now over the lack of good information for existing sites and the work involved in having to change themes without warning just as I was about to launch a new site, I just can't see the wood from the trees anymore. It's probably something really simple; I'd appreciate a strong pointer.............thanks Nigel

#908896

I've no idea what to do with this one either (this came from Luo originally):-

add_filter('cred_notification_recipients', 'ts_cred_notification_recipient_428', 10, 4);
function ts_cred_notification_recipient_428($recipients, $notification, $form_id, $post_id){
    
    if (428 == $form_id){
        $parent_slug = "parentpost";
        $field_slug = '_wpcf_belongs_' . $parent_slug . '_id';
          
        $parent_post_id = get_post_meta($post_id, $field_slug, true);
        $post_email = get_post_meta($parent_post_id, 'wpcf-email-address-parentpost', true);
          
        $recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$post_email );
    }
    return $recipients;
}

Can you do your magic please? I think once I see the general format for extracting the parent post elements, I'll be able to build on it (as I've done previously)

#908897

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Julie

If you have changed the markup for your form and it is using a relationship that you defined at Toolset > Relationships, then the second argument for toolset_get_related_post should be the slug of the relationship rather than an array of the post types, which is intended for legacy relationships.

Try changing that argument to the relationship slug.

If that doesn't work let me know and I'll set up a test.

#908900

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

For that second one, you again need to replace the line $parent_post_id = get_post_meta(... with a call to get the parent post ID using toolset_get_related_post.

If you fix the first one by changing the array argument to the relationship slug then I think you should be able to fix this second one in a similar fashion, but let me know if you get stuck.

#908981

Whoopee Nigel thank you for the pointers got both to work now! Soo relieved

I think it would help others if it were clear on this page: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post that the example is intended for legacy relationships.

For the record and if anyone else needs it, here's the two bits of amended code:-

To update child post title with parent post title:-

add_action('cred_save_data', 'save_data_form_3319',10,2);
function save_data_form_3319($post_id, $form_data){
 
    if ($form_data['id']==3319) {
    $parentpost_id = toolset_get_related_post($post_id, 'relationshipslug' );
    $parent_title = get_the_title($parentpost_id);
    $childpost_title = array(
        'ID'           => $post_id,
        'post_title'   => $parent_title,
    );
   wp_update_post( $childpost_title );
}
}

To send email to parent post author when child form submitted:-

add_filter('cred_notification_recipients', 'ts_cred_notification_recipient_3319', 10, 4);
function ts_cred_notification_recipient_3319($recipients, $notification, $form_id, $post_id){
     
    if (3319 == $form_id){
          
        $parent_post_id = toolset_get_related_post($post_id, 'relationshipslug' );
        $post_email = get_post_meta($parent_post_id, 'wpcf-email-address', true);
           
        $recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$post_email );
    }
    return $recipients;
}

Thanks again