Skip Navigation

[Resolved] Trying to access relationship value when saving CRED form..

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

Problem:
Trying to access relationship value when saving CRED form..

Solution:
You can access the parent field value using the Toolset form hook "cred_save_data" once you submit the form.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/trying-to-access-relationship-value-when-saving-cred-form/#post-1777367

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 4 years, 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Gerard 4 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1776243

I have 2 CPTs:
* Student Profile
* Grades

I have a Post Form that uses a relationship between the 2 CPTs above.
It allows people with the Manager role to enter a grade for a student.

In the form, the student is selected using this:
<div class="form-group col-md-12">
<label>Student's Name</label>
[cred_field field='@profile-grade.parent' class='form-control' output='bootstrap' select_text='Select Student Name...']
</div>

That grade, once entered by a manager, SHOULD be saved having the Student as author.

Its title should be made up of the first/last name of the student + 2 more fields .

The code I have so far is this:

// Form to add grades from the My Admin page for Managers
if ($form_data['id']==826)
{

list($fields,$errors)=$form_data; // I forget why I use this, but seems required!! 🙂

//uncomment this if you want to print the field values
//print_r($fields);

// Replace the source below with the
$first_name = get_the_author_meta('user_firstname'); // Needs to be from Parent post
$last_name = get_the_author_meta('user_lastname'); // Needs to be from Parent post
$session = $_POST['wpcf-session'];
$year = $_POST['wpcf-year'];
// $my_author = new WP_Query('author');

//$parent_post_id = $_POST['@relationship-slug_parent'];
//$parent_author_id = get_post_field ('post_author', $parent_post_id);

$my_post = array(
'ID' => $post_id, //?????
// 'post_author' => $parent_author_id,
'post_author' => $profile_id,
'post_title' => $last_name . ', ' . $first_name . "-" . $session . "-" . $year,
'post_name' => $last_name . ', ' . $first_name . "-" . $session . "-" . $year
);

wp_update_post( $my_post );
}
}

add_action('cred_save_data', 'gg_save_data_action', 999, 2);

So, my goal is to capture the First/Lastname values from the Profile CPT in the relationship, as well as the profile’s author, in order to save this new grade entered by a manager.

How do I access the profile CPT from functions.php?

I hope the above makes sense?

#1777367

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand "Student Profile" is your parent post type and you want to get the fields/title of your parent post type "Student Profile" when you submit the form for the post type "Grades".

If this is correct - as the parent field is added to your form:

[cred_field field='@profile-grade.parent' class='form-control' output='bootstrap' select_text='Select Student Name...']

So, you can access the selected parent (student) using the following line of code:

$parent_post_id = $_POST['@profile-grade_parent'];

Now, based on the $parent_post_id you should try to get the parent field information and get the value and adjust your code to build the post title.

I hope this is clear and please let me know if you will require further assistance.

Please correct me if I misunderstood your query.

#1788165

I think you understood. I'm back at it now, trying to work it out 🙂

The goal is to extract the proper details to save a post, which ar the Profile owner, first and last name.

Profile owner to save this current post as THEM.
The first-last name I use in the title..

#1789891

I got it done! Woohoo!

Thanks for that Minesh 🙂