Skip Navigation

[Resolved] How to retrieve CPT post title of 'related' post to use in another post

This support ticket is created 3 years, 7 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
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)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 3 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#2028665

Tell us what you are trying to do?

- I have a custom post type called 'Client'
- I have a custom post type called 'Reference'
- I have a custom post type called 'Customer'
- I have a custom post type called 'Activity'

- A 'Client' has a 'one-to-many' relationship with 'Reference' (ie a Client can have many References input onto the system). The Relationship slug is 'client-reference'.
- A 'Customer' can look at a 'Reference' and this then gets recorded as an 'Activity'. The recording of this Activity is processed manually by a website admin inputting an 'Activity' post into WordPress via a Post Form on the front end of the website (Website staff are notified that a 'Customer' has viewed a 'Reference' by a separate plugin, just for information purposes here).
Therefore, a 'Customer' has a 'one-to-many' relationship with Activity (a Customer can have multiple 'Activities'). The Relationship slug is 'customer-activity'.
- 'Reference' has a 'one-to-many' relationship with 'Activity' (ie a Reference can have many 'Activities' recorded). The Relationship slug is 'reference-activity'.

- As already mentioned above, an 'Activity' is input manually via a Post Form on the front end (the 'Activity Input Form'). All I want to do here, is to enter the 'Reference' that has been viewed, and the 'Customer' which viewed the reference (as well as a date, hour and minute that the activity happened).
This post form allows me to capture the 'Reference' by using the 'reference-activity' relationship field, this is a dropdown which allows me to pick the 'Reference' (this reference is the 'post_title' of the 'Reference' Post).
The Activity Input Form also allows me to capture the 'Customer' by using the 'customer-activity' relationship field, this also displays as a dropdown, where I can then select the 'Customer'.
The Date, Hour and Minute are simply 3 custom fields (a Date field and 2 select fields), and these get manually entered by the user as well.

Hopefully this all makes sense so far 🙂

So, what I am trying to do is, when I submit the Post Form (to create an 'Activity' post), is to then dynamically save the post title in the following format:

Reference Post Title_Date (YYYY-MM-DD)-Hour-Minute (so far example TEST1_2021-04-21_12-00 if the Reference was 'TEST1' and the activity happened today at 12 noon).

AND THIS IS MY ISSUE - Everything works fine, except for the 'Reference Post Title' part, this just returns "0". This is the code I am using to try and achieve what I want (this is in my child theme functions.php file):

add_action('cred_save_data', 'func_custom_post_title',10,2);
function func_custom_post_title($post_id, $form_data)
{

if ($form_data['id']==433) {

$km_post_ref = toolset_get_related_post($post_id, 'reference-activity', 'parent');
$km_date = date("Y-m-d", get_post_meta( $post_id, 'wpcf-activity-date', true ));
$km_hour = get_post_meta( $post_id, 'wpcf-activity-hour', true );
$km_minute = get_post_meta( $post_id, 'wpcf-activity-minute', true );
$args = array(
'ID' => $post_id,
'post_title' => $km_post_ref . '_' . $km_date . '_' . $km_hour . '-' . $km_minute
);
wp_update_post($args);

}
}

So, just to clarify, when I save my Activity Post, instead of being as follows:

TEST1_2021-04-21_12-00

The post in fact saves as:

0_2021-04-21_12-00

So the 'toolset_get_related_post' part is not working, and is just returning 0.

I currently have another support thread open (https://toolset.com/forums/topic/how-to-change-post-author-of-cpt-via-post-form-to-a-custom-field-value-on-save/) which seems to have a similar issue - so maybe there is a bug??

Is there any documentation that you are following?

So I am following guidance from this comment on my other post = https://toolset.com/forums/topic/how-to-change-post-author-of-cpt-via-post-form-to-a-custom-field-value-on-save/#post-2026519

I have also tried this URL = https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
In the 'More usage examples' code, where it seems to suggest removing the 'parent' part, but this does not work either.

Is there a similar example that we can see?
No, but I have made a screen recording of the error, so you can see it in action, which you can view here:

hidden link

What is the link to your site?

You have a link and also an Admin login, AND FTP login details on the other support thread link here (in the private box) = https://toolset.com/forums/topic/how-to-change-post-author-of-cpt-via-post-form-to-a-custom-field-value-on-save/#post-2027325

**I would really appreciate an answer on the other thread too as this is getting very urgent now, I cannot launch my project until this issue is fixed.

Thank you very much for your help 🙂

Keith

#2028775

AND THIS IS MY ISSUE - Everything works fine, except for the 'Reference Post Title' part, this just returns "0".
Hi, since the new relationships features were established in Types 3.0, the parent post(s) selected in a front-end Form are not yet established in the post relationships system by the time cred_save_data is triggered. That hook runs a bit too soon, so if you're trying to use the Post Relationships API toolset_get_related_post or toolset_get_related_posts in the callback to determine the parent post, it's best to use the cred_submit_complete hook instead:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

By the time cred_submit_complete is triggered, the selected parent post relationships have been fully established and the Post Relationships APIs will be able to grab the parent post successfully. However, you should note that in this code the output of toolset_get_related_post will never be the title of the post. It will be either the related post ID or 0:

$km_post_ref = toolset_get_related_post($post_id, 'reference-activity', 'parent');

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

See in the documentation:
Output: int – Post ID or zero if no related post was found.

If you want to use the title of that post, you'll need to call get_the_title or something similar on the returned post ID:

$km_post_id = toolset_get_related_post($post_id, 'reference-activity', 'parent'); // id of related post, or 0 if not found
$km_post_ref = get_the_title($km_post_id); // title of related post

Please try rewriting your code to use the cred_submit_complete hook instead of cred_save_data, and be sure to update the code to fetch the post title using the returned post ID value. I took a quick look at the other ticket you mentioned and I believe the issue there is similarly related to the timing of cred_save_data and the post relationships APIs. Jamal is usually off on Wednesdays and Thursdays so I suspect you won't get immediate feedback over there. I suggest changing the code to use the cred_submit_complete hook instead of cred_save_data anytime parent posts are selected in a child post Form and you want to access those related parent posts with the Post Relationships API in some Forms API callbacks.

#2028803

thank you so much Christian 🙂
(Also for updating me on the other ticket situation - I guessed they must be related). I will try what you have said above and report back shortly with an update. Thanks again I really appreciate it

#2028825

Sounds great, I'll stand by for your updates.

#2028931

Hi Christian,

I just wanted to let you know that your suggestion of changing from the 'cred_save_data' hook to the 'cred_submit_complete' hook (along with using 'get_the_title' with the post ID etc) worked perfectly - THANK YOU THANK YOU THANK YOU 🙂

I tried to apply the same thinking to my other ticket and modified my code (I have posted an update there so as not to confuse things https://toolset.com/forums/topic/how-to-change-post-author-of-cpt-via-post-form-to-a-custom-field-value-on-save/#post-2028929), but unfortunately that did not work 🙁
I don't know if you are able to help on the other ticket? Or if I need to wait for Jamal to return to work?

But regardless, thank you so much for the excellent help on this ticket 🙂

#2028945

Sure, I have taken over that ticket and followed up there, we can close out here.