[Resolved] Can't access parent post fields for PHP function and form fields
This thread is resolved. Here is a description of the problem and solution.
Problem:
The customer reported that the custom code linked to the 'cred_save_data' hook is not working correctly to get the custom field values from the related parent post and set them in the newly created post's title.
Also, the shortcode to include the custom field values from the related parent post in the form's email notification is also not working.
Solution:
After troubleshooting, suggested some changes in the custom function and the shortcodes for the related parent's custom field values.
I'm having trouble figuring out how to access the fields of a parent post when a user fills out a form with a one-to-many parent-child relationship. I was able to create a PHP function in order to send an email to the parent post author, but I can't seem to access the parent post fields for anything else.
What I need is to pull a couple of fields from the parent post to display in the message that I'm displaying after the form is submitted as well as in the notification emails and the post title that my cred_save_data function creates. I've tried every combination I can think of for post selection to get the fields to display and nothing has worked.
In case it's helpful, here's the functions code I'm using:
//Create a dynamic post title by the CRED form for speaker inquiries
add_action('cred_save_data','func_custom_inquiry_profile_title',10,2);
function func_custom_inquiry_profile_title($post_id,$form_data) {
if ($form_data['id']==51179) {
$inquiry_name = get_post_meta($post_id, 'wpcf-inquiry-first-name', true);
$parent_first_name = get_post_field('wpcf-first-name', $parent_post_id);
$parent_last_name = get_post_field('wpcf-last-name', $parent_post_id);
$parent_name = $parent_first_name . ' ' . $parent_last_name;
$speaker_name = get_post_meta($post_id, '$parent.first-name', true);
$title = $inquiry_name . ' for ' . $parent_name;
$args = array('ID' => $post_id, 'post_title' => $title);
wp_update_post($args);
}
}
/*Send email notification to speaker when inquiry is submitted*/
add_filter('cred_notification_recipients', 'send_speaker_inquiry_email', 10, 4);
function send_speaker_inquiry_email($recipients, $notification, $form_id, $post_id){
if (51179 == $form_id){
$author_id = get_post_field ('post_author', $parent_post_id);
$post_email = get_the_author_meta( 'user_email', $author_id );
$recipients[] = array(
'to'=>'to',
'address'=>$post_email,
'name'=>'',
'lastname'=>''
);
}
return $recipients;
}
I've tried a couple different iterations of the $speaker_name arguments, so I know that the way it's set up now is probably incorrect. I'm just stumped as to how to reference the parent post fields correctly at this point.
Thank you for contacting us and I'd be happy to assist.
Looking into the code snippet that you've shared, it is not clear how you're trying to use the parent post's ID from the variable '$parent_post_id'.
Can you please share temporary admin login details, along with the link to the page with this form? I'll be in a better position to guide you with the next steps, accordingly.
Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.
I changed out the code and ran a test form submission, but it's still not working. It's still not pulling the parent post fields for the notifications, either, which is possibly an even bigger issue, honestly. Please feel free to go ahead and create the clone/snapshot. Thank you.
Thank you for the permission and I've downloaded the website's clone.
During testing on the website's clone and your actual website, I noticed that the first part of the code that sets the 'Inquiry' post's title is working as expected.
To confirm, I visited the form page ( {yourwebsite.com}/submit-speaker-inquiry/?parent_speakerprofile_id=48492 ) and submitted with the first name "Test Fname". The created title "Test Fname for Jenn Zellers" automatically included the title of the parent speaker profile post "Jenn Zellers".
( it is important that the correct id of the speaker profile post is available in the URL e.g. "parent_speakerprofile_id=48492" )
I'm currently working on the part of accessing the related post's data in the form's notification and will update you, as soon as this testing completes.
Ah, that makes sense. This particular form shouldn't be accessible to the public except through the link that creates an automatic relationship, so that's great; it should always have the correct ID in the URL.
Thanks for the update on the post title and I'll look forward to seeing if the notifications are able to be sorted. I really appreciate the help!
Awesome - that makes sense! I was trying all sorts of different shortcode references, but I must have missed that one. Thank you so much for your help on this!
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.