Skip Navigation

[Resolved] CRED: set relationship trough predefined options

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

Problem:
CRED: set relationship trough predefined options

Solution:
You can use the Post relationship API function "toolset_connect_posts" in order to establish relationship between your post dynamically.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/cred-set-relationship-trough-predefined-options/#post-1093257

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

This support ticket is created 6 years, 4 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
- 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 5 replies, has 2 voices.

Last updated by matijaK 6 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1092853
Capture4.PNG
capture3.PNG
Capture2.PNG
Capture.PNG

Hi,

I have a website for courses with following post types: course / course-date / course-price / application

I have a booking form that enables users to create a new application to a course.
The form is embedded in a course page and creates a new application whose relationship is set to the course on submit using a generic field for field "@course.application-parent" with default value "default":"[wpv-post-id]". This solution works fine.

My users can also select a starting date they preffer and a price while they submit this form. See attached image (Capture).
The starting dates and prices are defined as related posts to this course.
The form displays the related dates and prices trough a VIEW that generates a JSON string set that is displayed in the form trough a generic field with options. (again see attached image for clarification - Capture2).

When the price and dates are selected, a script in my theme's functions.php writes the selected option into a field as an ID of the related post, using: if (isset($_POST[select-a-price])) $_POST['wpcf-selected price']=$_POST['wpcf-select-a-price'] (see attached image Capture3).

My aim is for the new application submitted by this from to have the same related price and course start date as the parent course.
Can I use the same solution as described above and write into the relationship table (toolset_associations)? (see image capture 4).

to sum up:
Course
-> Related posts: price(s)
-> Related posts: course date(s)
FORM: generate new Application with parent Course.
GOAL: add same related posts to new Application as parent Course

#1092886

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - post relationship offers the post relationship API function to connect related posts.

Please check following Doc:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

For example:

// Connect a custom Author post with the ID of "5" to a custom Book post with the ID of "7" in a post relationship between Books and Authors
toolset_connect_posts( 'book-author', 5, 7 );
#1093224

Hello Minesh,

thank you for a fast reply. I've checked the link you send, but I'm still not entirely clear on how to invoke this function from the form described in the above situation. Can you please show me in the right direction?

#1093257

Hi Minesh,

I figured it out searching for the toolset_connect_posts.
By the way, you guys could really benefit from a better search engine in the forums, as I could only find this thread on google.

This is the thread I used for refference https://toolset.com/forums/topic/filtered-parent-dropdown/ as it's very similar to what I'm doing, thanks to Christian for explaining it so neatly again.

The proposed solution works

add_action('cred_save_data', 'cred_connect_applications',10,2);
function cred_connect_applications($post_id, $form_data)
{
    $forms = array( 295 );
    if (in_array($form_data['id'], $forms))
    {
        if (isset($_POST['wpcf-select-price']))
        {
            $parent_id = $_POST['wpcf-select-price'];
            $parent = toolset_connect_posts( 'price-application', $parent_id, $post_id );
        }
		
		if (isset($_POST['wpcf-select-date']))
        {
            $parent_id = $_POST['wpcf-select-date'];
            $parent = toolset_connect_posts( 'date-application', $parent_id, $post_id );
        }
    }
}
#1093802

Minesh
Supporter

Languages: English (English )

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

By the way, you guys could really benefit from a better search engine in the forums, as I could only find this thread on google.
=> I will pass your feedback to concern department.

Good to know and thanks for sharing solution.

#1099391

Closing this topic as resolved.