Sauter la navigation

[Résolu] Set a child post title from parent post titles with code

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

Problem: I have a Form that creates posts and allows the User to choose two post parents from other post types. I would like to use the Forms API to automatically set the child post title to include the title of both parent posts.

Solution:
Add this hook:

add_action('cred_save_data', 'your_save_data_action',10,2);
function your_save_data_action($post_id, $form_data)
{
  // if a specific form
  if ($form_data['id']==138)
  {
    $docyear = get_post_meta($post_id, 'wpcf-year', true);
        $custname = isset($_POST['@customer-requirement_parent']) ? get_the_title($_POST['@customer-requirement_parent']) : '';
        $docname = isset($_POST['@document-master-list-requirement_parent']) ? get_the_title($_POST['@document-master-list-requirement_parent']) : '';
        $drdetail= $docyear. ' - ' . $custname. ' - ' . $docname;
        $args = array('ID' => $post_id, 'post_title' => $drdetail);
        wp_update_post($args);
  }
}

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

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

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

Dernière mise à jour par AndreG3332 Il y a 6 années.

Assisté par: Christian Cox.

Auteur
Publications
#1154015

I have a few posts where I set the post title with the following type of code:

/*This functions add the contat person name to the post title of the contat person post */
	if ($form_data['id']==43) {
        $surname = get_post_meta($post_id, 'wpcf-surname', true);
        $firstname = get_post_meta($post_id, 'wpcf-first-name', true);
        $cptitle= $surname. ' - ' . $firstname;
        $args = array('ID' => $post_id, 'post_title' => $cptitle);
        wp_update_post($args);
    }

And it worked 100% now I am trying to do the same but with little success.

The scenario: A CPT with two relationships, one with Customers and one with document master list. I created a post form that is completed and with the relationships as set out above and works 100%, however, I am not able to set the post title with the following code

/**This function add the Document requirements per year per clients  to the post title of the documents required per client */
	if ($form_data['id']==138) {
		$docyear = get_post_meta($post_id, 'wpcf-year', true);
        $custname = get_post_meta($post_id, 'wpcf-customer-requirement', true);
        $docname = get_post_meta($post_id, 'wpcf-document-master-list-requirement', true);
        $drdetail= $docyear. ' - ' . $custname. ' - ' . $docname;
        $args = array('ID' => $post_id, 'post_title' => $drdetail);
        wp_update_post($args);
    }	

A link to access the form is

lien caché

Here is the code generated for the form:

[credform]
	[cred_field field="form_messages" class="alert alert-warning"]
	<div class="form-group">
		<label>Year</label>
		[cred_field field="year" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Customers Requirements</label>
		[cred_field field="@customer-requirement.parent" class="form-control" output="bootstrap" select_text="--- not set ---"]
	</div>
	<div class="form-group">
		<label>Documents Master List Requirements</label>
		[cred_field field="@document-master-list-requirement.parent" class="form-control" output="bootstrap" select_text="--- not set ---"]
	</div>
	[cred_field field="form_submit" output="bootstrap" value="Submit" class="btn btn-primary btn-lg"]
[/credform]

With the above code, it will create a post name of "2018--" instead of "2018 - name - filename"

I have tried to replace "wpcf-document-master-list-requirement" with "@document-master-list-requirement.parent" with no success either. If you could please point me to the correct code to achieve this.

#1154097

Hi, are the "name" and "filename" supposed to be the titles of the related parent posts? If so, that information is not stored in postmeta. The parent fields capture post IDs, so you can access the corresponding post titles using the $_POST superglobal. The syntax is a bit different, like this:

add_action('cred_save_data', 'your_save_data_action',10,2);
function your_save_data_action($post_id, $form_data)
{
  // if a specific form
  if ($form_data['id']==138)
  {
    $docyear = get_post_meta($post_id, 'wpcf-year', true);
        $custname = isset($_POST['@customer-requirement_parent']) ? get_the_title($_POST['@customer-requirement_parent']) : '';
        $docname = isset($_POST['@document-master-list-requirement_parent']) ? get_the_title($_POST['@document-master-list-requirement_parent']) : '';
        $drdetail= $docyear. ' - ' . $custname. ' - ' . $docname;
        $args = array('ID' => $post_id, 'post_title' => $drdetail);
        wp_update_post($args);
  }
}

*edit - removed some extra spaces*

#1154463

My issue is resolved now. Thank you!