Skip Navigation

[Résolu] get parent post data and create child post title using CRED form

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

Problem:

Use a CRED form for creating child post, I need to get parent post information within cred_save_data action hook using PHP codes.

Solution:

In the CRED form for creating child post, there is a a parent post selector, it will pass a POST parameter "@client-client-portfolio_parent" to target page, so you will can get the parent post ID with below codes:

$parent_post_id = $_POST['@client-client-portfolio_parent'];

Relevant Documentation:

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

This support ticket is created Il y a 5 années et 12 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
- 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/Hong_Kong (GMT+08:00)

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

Dernière mise à jour par davidZ-4 Il y a 5 années et 12 mois.

Assisté par: Luo Yang.

Auteur
Publications
#1161296

Tell us what you are trying to do? create a child post using a from and give it a dynamic name with data from the parent post

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/displaying-parent-posts/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/forums/topic/cannot-get-parent-post-id-on-cred_save_data-with-new-relationships-api/#post-1121934

Is there a similar example that we can see?
not sure

What is the link to your site?
lien caché

I have tried multiple solutions but couldn't get this to work.
i can create a dynamic name for a post but cannot get the parent data to insert to the child post.
both parent and child posts are with status Private

here is one of the codes i tried

//Create a dynamic post title by the CRED form.
add_action('cred_save_data', 'func_custom_post_title', 10, 2);
function func_custom_post_title($post_id, $form_data)
{
	$author = get_the_author();
	$user_id =  get_the_author_meta('ID');
	//if new client - parent post dynamic name works just fine
	if ($form_data['id'] == 283 ) {
		$author = get_the_author();
		$user_id =  get_the_author_meta('ID');
		$post_type = get_post_type( $post_id ); 
		$cfn = get_post_meta($post_id, 'wpcf-client-first-name', true);
		$cln = get_post_meta($post_id, 'wpcf-client-last-name', true);
		$cshrtid = get_post_meta($post_id, 'wpcf-client-id-number', true);
		$title = $cfn.' '.$cln.' '.$user_id.'-'.$cshrtid.'-'.$post_id;
		$slug = sanitize_title($title);
		$args = array('ID' => $post_id, 'post_title' => $title, 'post_name'=>$slug);
		wp_update_post($args);
	}
	//if new portfolio -child post cannot get the parent post data so this is not working
	if ($form_data['id'] == 386 ) {
		
//get data from parent post i tried this option
		$parent_post_id = toolset_get_related_post( $post_id, 'client-client-portfolio', 'client' );		
		
// and i tried this option
		$child_post_id = get_post($post_id);
		$parent_post_id = toolset_get_related_post( $child_post_id, 'client-client-portfolio', 'parent');
		
		$cfn = get_post_meta($parent_post_id, 'wpcf-client-first-name', true);
		$cln = get_post_meta($parent_post_id, 'wpcf-client-last-name', true);
		$cshrtid = get_post_meta($parent_post_id, 'wpcf-client-id-number', true);
		//get data from current post		
		//$post_type = get_post_type( $post_id );
		// Get the title of the parent (client) post
		$title = $cfn.' '.$cln.' '.$user_id.'-'.$cshrtid.'-'.$parent_post_id.'-P'.$post_id;
		$slug = sanitize_title($title);
		$args = array('ID' => $post_id, 'post_title' => $title, 'post_name'=>$slug);
		wp_update_post($args);
	}
}

please advise,
thanks,
David

#1161384

Dear David,

I assume we are talking about same website as your previous thread:
https://toolset.com/forums/topic/display-parent-data-on-a-child-post/#post-1157554

I have checked the database dump file again, in the form ID 386, there is a hidden parent post selector, after user submit the form, it will pass a POST parameter "@client-client-portfolio_parent" to target page, so you will can get the parent post ID with below codes:

$parent_post_id = $_POST['@client-client-portfolio_parent'];
#1161616

My issue is resolved now. Thank you!