Skip Navigation

[Resolved] Use parent post title to create child post’s title with CRED

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

Problem: I am using CRED to create child posts. I would like to programmatically set the child post title based on the parent post title. I'm using the beta plugins.

Solution:
The post data key for accessing the parent post's ID has changed in the M2M betas. You can now use the format "@" + relationshipslug + "_parent". In this case the relationship slug is member_visitation, so the key is "@member_visitation_parent".

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']==429)
{
if (isset($_POST['@member_visitation_parent']))
{
$my_post = array(
'ID' => $post_id,
'post_title' => get_the_title($_POST['@member_visitation_parent'])
);
wp_update_post( $my_post );
}
}
}

0% of people find this useful.

This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

This topic contains 13 replies, has 2 voices.

Last updated by Christian Cox 2 years, 10 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#681154

I am trying to: Automatically use the title from my parent post type (Members) to use as the title for the child (Visitation Cards).

Link to a page where the issue can be seen: Page fulfills the CRED form submission however on the post it creates, the title shows (no title).

I expected to see: Parent title

Instead, I got: (no title)

Here is what I have so far.

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']==429) {
if(!empty($_POST['_wpcf_belongs_member_id'])){
$name = $_POST['_wpcf_belongs_member_id'];
}

$title = $name;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

#681491

I've also used this from another support ticket recommendation, but cant get things to work.

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 );
}
}
}

#681507

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-05:00)

The second one looks better because the first one does not actually do anything to get the parent post's title. This line should do that for you:

'post_title' => get_the_title($_POST['_wpcf_belongs_parentslug_id'])

So as a test, please start with the 2nd example you gave. Hard-code the post title like this:

{
$my_post = array(
'ID' => $post_id,
'post_title' => 'Test static title'
);

Replace 12345 with the ID of this CRED form, and replace "parentslug" with "member". Then submit the form once more and check the child post's title. It should be "Test static title". If it's not, then we need to take a closer look.

#681540

It did not return the static text. Here is my complete funciton.php file customizations. The visitation cards is where we are working. The other two work fine.

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:

// END ENQUEUE PARENT ACTION

//Member Create Automatic Title Generator, based on First and Last Name Custom Fields
add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
// if a specific form
if ($form_data['id']==477) {
if(!empty($_POST['wpcf-member-first-name'])){
$first_name = $_POST['wpcf-member-first-name'];
}
if(!empty($_POST['wpcf-member-last-name'])){
$last_name = $_POST['wpcf-member-last-name'];
}
$title = $first_name .' '. $last_name;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

//Visitation Card Create Title Generator, based on relationship selection drop down box for person requiring visit
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']=='429')
{
if (isset($_POST['_wpcf_belongs_member_id']))
{
$my_post = array(
'ID' => $post_id,
'post_title' => 'static title test'
//get_the_title($_POST['_wpcf_belongs_member_id'])
);
wp_update_post( $my_post );
}
}
}

// automatically assign a slug to all posts, specified in array below by ID, created by CRED
add_action('cred_save_data', 'set_term_for_cred_post',10,2);
function set_term_for_cred_post($post_id, $form_data) {
// array of cred form IDs and corresponding term slugs
$array = [
'429' => 'on',
];
$taxonomy = 'visitation-status';
$tag = array( $array[$form_data['id']] );
wp_set_object_terms( $post_id, $tag, $taxonomy, true );
}

#681542

It gave me the title:

CRED Auto Draft 051454f749fffb293acd259214743d38

#681543

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-05:00)

Okay my guess is that one of these conditionals is blocking the update for some reason:

if ($form_data['id']=='429')
{
if (isset($_POST['_wpcf_belongs_member_id']))
{

Try this:
- Verify that the CRED form ID is really 429
- Remove the quotes around 429
- Check your CRED form to ensure the parent post field really has the slug "_wpcf_belongs_member_id", and be sure something is selected here.

If nothing seems to work, we can try to add some server logs next and see what's going on.

#681546
Screen Shot 2018-04-17 at 4.49.59 PM.png
Screen Shot 2018-04-17 at 4.49.01 PM.png
Screen Shot 2018-04-17 at 4.44.40 PM.png

Here is my post form codes. Create Visitation is 429.... check.

Removed '' from around 429 and tried, no luck.

Check your CRED form to ensure the parent post field really has the slug --- Not sure what you mean here.

"_wpcf_belongs_member_id", and be sure something is selected here. --- What does that mean? Sorry!

Also uploaded a few screenshots to show you a few setting in my wordpress.

#681567

You can see how I'm relating the child to the parent in the form with:

<div class="form-group">
<label>Name</label>
[cred_field field='@member_visitation.parent' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>

This is selected. Ultimately, this is what the Title should be on the Child, is this value.

#681569

I have a relationship setup like this:

Member (1) to Visitations (Many)
Visitations (1) to Responses (Many)
Member (1) to Responses (Many)

Its kind of a loop. The Members may need visiting and a "visitation card" is created for them. That card is attached to them. Then the Title of this card should be that members title (parent).

The Visitation Card, then gets many response cards connected to it. Ironically, the members also should be attached to these responses.

The members are needing visits by other members. And I'm trying to connect them so we can know the open visits that need to be made, who visited a members, and how many visits a particular member has made etc...

Hope that makes sense. Just trying to give good example for anyone else who will run into similar situation.

#681801

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-05:00)

Okay I see now, you're using the betas. For future reference it's a good idea to be clear about that when you submit a ticket, because there have been significant technical changes. The code you're using here is not designed for use with post relationships created in the beta plugins. The API for querying post relationships in the new system is a bit more complex, and the $_POST information collected by CRED has changed format. Instead of _wpcf_belongs_member_id, you can access the parent post ID in the post key @member_visitation_parent. Try this version:

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']==429)
{
if (isset($_POST['@member_visitation_parent']))
{
$my_post = array(
'ID' => $post_id,
'post_title' => get_the_title($_POST['@member_visitation_parent'])
);
wp_update_post( $my_post );
}
}
}
#681824

Yikes! Sorry, yes... I wanted to get a jump start on this because I knew it would be released in future and my project wouldnt be complete for a while so I wanted to try and learn and build as I go with the betas. Thanks and sorry for confusion. Let me see if this works.

#681855

Your rock man! So all you changed was how this was referenced right? With the @member_visitation_parent

Also, will what I create in beta migrate over ok to the new release when it comes out?

#681898

Just wanted to get confirmation on the above. I realized I had closed ticket out. Thanks. This worked great. I thought I tried this, but I also noticed you took off '' from 429. Didnt know to try that.

#681899

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-05:00)

Correct, that's all that changed in this case. More info about the upcoming Post Relationship API changes here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

It's not possible to programmatically set a post relationship yet, so you must rely on CRED or wp-admin to do that. You can query on post relationships though, and that's helpful.

The forum ‘Types Community Support’ is closed to new topics and replies.