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