Skip Navigation

[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.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

This support ticket is created 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#2290481

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.

#2291497

Hi,

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.

regards,
Waqar

#2292577

Thank you for sharing these details.

Reviewing the setup and the code, you can get the ID of the selected parent 'Speaker Profiles', using '$_GET['@speaker-inquiries.parent_disabled']'.

Can you please test this updated code snippet from your first reply?


//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) {
        // get the parent post's ID
        $parent_post_id = $_GET['@speaker-inquiries.parent_disabled'];

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

#2294113

I changed out the code snippet and it still doesn't pull the parent post fields, unfortunately.

#2295551

Can you please replace the line:


$parent_post_id = $_GET['@speaker-inquiries.parent_disabled'];

With:


$parent_post_id = $_GET['parent_speakerprofile_id'];

This should make the code set the title for the created "Inquiry" post using the first and last name from the parent post.

In case, this still doesn't work, I'll need your permission to download a clone/snapshot of the website, to investigate this on a different server.

#2295629

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.

#2298095

Are there any updates on this?

#2298393

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.

#2298907

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!

#2299555

Thank you for waiting.

I was able to make the fields in the notifications work, by making the following changes:

1. Host Notification

- In the subject line, I changed the first name field shortcode for the parent speaker post, to:


[types field='first-name' item='@speaker-inquiries.parent'][/types]

And in the notification message, changed the shortcodes for the speaker's first and the last name to:


[types field='first-name' item='@speaker-inquiries.parent'][/types] [types field='last-name' item='@speaker-inquiries.parent'][/types]

Please note the usage of item='@speaker-inquiries.parent' attribute.
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/ )

2. speaker-notification

- In this notification's body, changed the shortcode for the speaker first name to:


[types field='first-name' item='@speaker-inquiries.parent'][/types]

#2300465

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!