Skip Navigation

[Résolu] Add Parent Post Title to Child Post Title Using cred_save_data Function

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have a CRED form that allows users to create child posts. Using cred_save_data, I would like to create a custom post title using information stored in a custom field on the parent post.

Solution: In cred_save_data, you will be able to determine the parent post's ID using get_post_meta to access _wpcf_belongs_parentslug_id. Then you can get the custom field value using get_post_meta, the parent ID, and the custom field slug:

...
// get the parent ID from the child postmeta
$parent_id = get_post_meta($post_id, '_wpcf_belongs_parentslug_id', true);
// get the title from the parent postmeta
$custom_title = get_post_meta( $parent_id, 'wpcf-custom-field', true );
...

Relevant Documentation: https://developer.wordpress.org/reference/functions/get_post_meta/
https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

This support ticket is created Il y a 7 années et 3 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 7 années et 3 mois.

Assisté par: Christian Cox.

Auteur
Publications
#562378

I have post types AAA and BBB. Post type BBB is a child of AAA. I want the post title of BBB to to be the same as AAA .

I use this function (elsewhere) to update a post title from a custom field in the same post

function save_data_form_100($post_id, $form_data){
            
            if ($form_data['id']==100){
                
               $custom_title = get_post_meta( $post_id, 'wpcf-custom-field', true );
                
                    $my_post = array(
                    'ID' => $post_id,
                    'post_title' => $custom_title,
                    'post_name' => $custom_title,
                    
                );
                
                wp_update_post( $my_post );
                
            }
        }
    add_action('cred_save_data', 'save_data_form_100',10,2);

How do I adjust this to either :-

1. update the post title of BBB with the post title of AAA

OR

2. update the post title of BBB with the value of a custom field in AAA

please?

I've trawled loads of other forum threads in search of the answer but nothing I've tried has worked.

Many thanks

#562460

Types defines the link between a child post and a parent post using a postmeta field '_wpcf_belongs_aaa_id' applied to the child post. So you can access any information about the parent post by getting the value of that postmeta field, and using that as an ID to query the parent post.

...
// get the parent ID from the child postmeta
$parent_id = get_post_meta($post_id, '_wpcf_belongs_aaa_id', true);
// get the title from the parent postmeta
$custom_title = get_post_meta( $parent_id, 'wpcf-custom-field', true );
...

Replace 'aaa' with the slug of your parent post type, and 'custom-field' with the slug of the parent post custom field.

#562663

Hi Christian

Many thanks for this - I was almost there and can see where my code needed adjusting.

Since posting, I've realised that actually using parent/child posts might not be the best approach in this instance. Before I close this thread, can I ask you about this scenario please?:-

Let's suppose there is no relationship between Post Type AAA and BBB. A post has been created using Type AAA and has an ID of 100 . A post using Type BBB is to have a post title using the value of wpcf-custom-field from the post with ID of 100. Is this achievable and how would I adjust my code for this please?

#562768

Instead of using a parent ID variable, you would hard-code the number 100 in its place:

...
// get the title from post 100's postmeta
$custom_title = get_post_meta( 100, 'wpcf-custom-field', true );
...

Now every post created by the CRED form will be assigned a custom title using the custom field from post ID 100.

WP documentation for get_post_meta: https://developer.wordpress.org/reference/functions/get_post_meta/

#563064

That's awesome Christian, thank you!