Skip Navigation

[Resolved] Automatically update a related post with data of the currently edited post, in Toolset Forms

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

Problem:
How can we update automatically the title or other content of a post related to the one we are currently editing in a Form so that the data of the currently edited Form is updated to the related Post(s), automatically, when we submit the Form?

Solution:
This is possible with Toolset Forms' API.
When working with Forms that both set a relationship to other posts, and the data of these edited posts should be used to then update the content of the related post, you should use the cred_submit_complete hook of Toolset Forms API.
In the hooks function, you should determine the related posts using the Toolset Relationships API.

Then, with the WordPress core method wp_update_post() you can update the related posts with content from the currently edited post.

A good example can be found here:
https://toolset.com/forums/topic/auto-populate-parent-title-and-date-as-child-content-title/#post-1282743

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://codex.wordpress.org/Function_Reference/wp_update_post
https://toolset.com/documentation/programmer-reference/cred-api/

This support ticket is created 4 years, 10 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 8 replies, has 2 voices.

Last updated by Beda 4 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#1279991

I have been trying to auto populate the parent post title and the date of the post as the title of child content. I've gone through a lot of support forums, and nothing seems to work. I have gotten close a few times...

I created a shortcode to display the date and integrated it with Toolset, no problem. I tried using this shortcode, which I saw on another support page (I'll link below):

[cred_field field="post_title" post="contactformulier" value="[cred_post_parent get='title'] - [wpv-post-today]"]

But my form outputs a blank title field, followed by a: "June 29, 2019]

This shortcode (without my added date shortcode) seems to have worked for another Toolset user, on this page: https://toolset.com/forums/topic/auto-fill-child-post-title-with-parent-title/ but even without the date shortcode it isn't working for me for some reason. Has Toolset's code changed in some way?

Link to a page where the issue can be seen: hidden link

#1280023

cred_post_parent is an older ShortCode and it won't work with modern relationships as those are many-ended meanwhile, while in past we had relatively strict relationships where always only one parent could be selected.
But the ShortCode still works for certain kind of relationships, as explained here https://toolset.com/forums/topic/in-many-to-many-relationship-cred_post_parent-getid-is-not-working-properly/
There are other solutions to access parent data as well: https://toolset.com/forums/topic/form-stopped-working-when-types-updated-to-3-2-1/

However, if you have a more complex relationship you may be working on a relationship form, which does not allow such approaches, also no Custom Code can be hooked to those.

This only will work if you have a One To Many Relationship and you are editing or creating related Child Posts.
Then you can pull data from the Parent Post you chose.

This would also be possible using the Toolset API for Relationships, within a cred_save_data() action (this is with PHP; when the Form saves it's data).
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://toolset.com/documentation/programmer-reference/cred-api/

Can you first try with the suggested edit of the shortcode as in https://toolset.com/forums/topic/in-many-to-many-relationship-cred_post_parent-getid-is-not-working-properly/?
That should solve the issue if it's a One to Many relationships where you are editing the Many part.

Otherwise, I'll show you an example of a custom code snippet for the Toolset Forms API, as seen here:
https://toolset.com/forums/topic/populate-child-post-title-with-parent-post-title/
https://toolset.com/forums/topic/get-parent-post-data-and-create-child-post-title-using-cred-form/
(and others)

Please let me know if the first solution does it already.

#1280189

This is a one-to-one parent child relationship, I should have clarified that before.

I tried this shortcode from the first option you gave, I edited the code to include the slug in my Toolset CPT, but that wasn't accepted as a value either. The quotation marks might be messing it up somehow?

<label>Vehicle Name and Date</label>
[cred_field field="post_title" value="[cred_post_parent get='title'] - [wpv-post-today]"]

Still comes up like this: hidden link

-----------------------------------------------------

This is another one I tried, but that didn't seem to work either. I got this one from the Toolset Fields:

<label>Vehicle Name and Date</label>
[cred_field field="post_title" value="[wpv-post-title item="@membership-vehicle-repair.parent"] - [wpv-post-today]"]

It did fix the syntax that was causing the date field to go outside the child's title field, so that's... something, haha. What's interesting though is that the quotation mark at the end of .parent" till the end of the line is red, like there is still a syntax error, even though there isn't.

-----------------------------------------------------

I tried doing it programatically using the functions.php file, but those required my form be on the same page as the View or parent content, which somehow broke the link between the child post and it's parent. Ideally I'd like to get the first option working so I don't have to redo all of my Views, content layouts, and the form.

#1281657

The code you show has no post type attribute set.

[cred_field field="post_title" value="[cred_post_parent get='title'] - [wpv-post-today]"]

It should be:

[cred_field field="post_title" value="[cred_post_parent get='title' post_type='your_post_type_slug'] - [wpv-post-today]"]

And yes, this may break in Forms due to the apostrophes.
Can you check if you get the expected values when you use only the shortcodes to display the values?

[cred_post_parent get='title' post_type='your_post_type_slug'] - [wpv-post-today]

On that form, do you now see the expected values?
If so, you can use cred_save_data() and use do_shortcode() for example, but if you are using cred_save_data then you can as well use toolset_get_related_post() to get the related posts and its title.

There are custom code snippet examples for this here:
https://toolset.com/forums/topic/cannot-get-parent-post-id-on-cred_save_data-with-new-relationships-api/#post-1121934
https://toolset.com/forums/topic/populate-child-post-title-with-parent-post-title/#post-908981
https://toolset.com/forums/topic/update-parent-field-when-submiting-child-post-form/

These snippets need to be adapted to your case, they will not work out of the box. The DOC to use the API is here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This is the best method to use if the cred_post_parent shortcode does not fully satisfy the needs.

#1282217

I tried it with the post type attribute and without, it didn't work with either of those. I shared the example of code without the post type attribute since this is a one-to-many relationship, and the instructions you shared indicated that the post type attribute was specifically for a many-to-many relationship.

Using the second article you shared, I updated it to have my form ID, and placed in it my functions.php file, and it mostly works.

add_action('cred_save_data', 'save_data_form_888',10,2);
function save_data_form_888($post_id, $form_data){
  
    if ($form_data['id']==888) {
    $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 );
}
}

The problem is that it is giving me the name of the page the form is on (Submit Repair Form), not the name of the parent vehicle. I also wasn't able to figure out how to include the current date in that function. I imagine it would be added to the array like this?

'post_title'   => $parent_title, date( 'F j, Y', current_time( 'timestamp' )),
}

Or maybe referencing the other function I am calling, more like this?

'post_title'   => $parent_title, $today_shortcode,

-----------------------------------------------

To try to solve the problem of it grabbing the wrong title, I tried to move the form from the "Submit Repair Form" page to the parent vehicle page, so that it would copy that title. Somehow that removed the link the form from the parent content. Here is the field that is no longer automatically linked:

[cred_field field='@membership-vehicle-repair.parent' class='form-control' output='bootstrap' select_text='--- not set ---']

I tried the other articles you offered as well, this was the closest I got to a solution.

#1282743

According to the DOC, you can either pass the Form ID to the function name itself or pass it as an IF statement.
You don't need to do both.
Hence your function could be:

//Add your method to the cred_save_data action
add_action('cred_save_data', 'ts_example_save_data',10,2);

//register your method
function ts_example_save_data($post_id, $form_data){
   
    //Change the 888 to the ID of your Toolset Form, where you want this code to apply
    if ($form_data['id']==888) {
    //Custom Magic here
    }

}

Now, if you want to get the parent post ID, you need to hook in a bit later than cred_save_data.
Please try this on cred_submit_complete(), as shown here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

This is because, at the point of saving the data, the relationship is not established yet, but it is when the to submit is complete.

So your code could look like this:

add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        
    }
}

To get the parent of the post you either edited or created with Forms here, you can use the toolset_get_related_post() function.

$parent_id = toolset_get_related_post( $post_id, 'relationships-slug');

If hooked to cred_submit_complete() this should do the job.

Can you confirm this solves the issue?

To add content to the post_title, you need to follow the WordPress Codex and basic PHP - here you would use concatenation to add more data such as a date.
For example:

//define today_shortcode
$today_shortcode = date("Y"); //whatever value, like in the example, see <em><u>hidden link</u></em>, <em><u>hidden link</u></em>
'post_title'   => $parent_title . ' ' . $today_shortcode,

Then this concatenates $parent_title, a empty space, and $today_shortcode to one string.

Please let me know if with these details you are able to craft the custom code.

#1285213

Hey, that did it!! I'll share the code for anyone else who wants to do something like this.

I did notice the date doesn't seem to be syncing to my site date/time or the server date/time. If I post something late at night the date comes up as the next day, so it seems like it is going off of UTC-0 instead of my site or server time.

To fix that, add the php function to set the default time zone first.

//////
//Set time zone
date_default_timezone_set('America/Denver');

/////////////
// Auto Populate Repair
add_action('cred_submit_complete', 'auto_populate_title',10,2);
function auto_populate_title($post_id, $form_data){
    // Edit form ID to match form ID
    if ($form_data['id']==888) {
        $parentpost_id = toolset_get_related_post($post_id, 'membership-vehicle-repair' );
        $parent_title = get_the_title($parentpost_id);
        $today_date = date("F j, Y");
        $childpost_title = array(
            'ID'           => $post_id,
            'post_title'   => $parent_title . ' - ' . $today_date,
        );
   wp_update_post( $childpost_title );
	}
}
// In this example, the form ID is 888, change that to match your form ID. 
// The relationship slug in this example is "membership-vehicle-repair." Change that slug to match your relationship slug (not the parent slug, a mistake I made at first)
#1285219

This works well, I simply hid the title field, with a default of the date, then this function comes in and corrects the title to be the parent first name + the publish date.

There may be a better way to display the site/server date, but this worked for me. If anyone can think of a better way (such as the current_time() function?), I'm open to it, but this works so I'm going to go with it, haha.

#1285375
date()

is a PHP method hidden link
It does not really "care" if it's used with WordPress, which offers a "Timezone" as setting, or in any other PHP framework:
It will always return date in the passed format using the given integer timestamp or the current time if no timestamp is given.

In your case, you use it without the timestamp integer

date("F j, Y");

You could pass a timestamp integer to the method like this:

date("F j, Y", $timestamp);
$timestamp

would have to be defined in your code first, by populating it with a valid timestamp.

But you could directly use the WordPress Core function

current_time()

, which returns the blogs current time - not UTC but what you set in settings.
Directly return it in yur desired Format, finally using this in your code to populate the title:

$today_date = current_time( 'Y-m-d' ); //any format from here should work: <em><u>hidden link</u></em>

See: https://codex.wordpress.org/current_time
Since current_time will return the local time for the timezone declared in the blog's Timezone setting on the General Settings page, this should solve the issue you faced in the last report.

Hope this helps!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.