I am trying to:
create a post form to connect posts that are in relationship.
Example: Course <--> Venues
The post form was configured in this manner:
[cred field='@course-venues.parent' ...]
I expected to see:
the post form select field to display connected venues that are connected to the course.
Instead, I got:
a full list of venues that are in venues post type.
create a post form to connect posts that are in relationship.
Hi, I'm a bit confused.
- What kind of relationship is this - one-to-many or many-to-many?
- Which post type is the parent, and which post type is the child? You can find out by editing this relationship in Toolset > Relationships. Click the relationship name, then check the confirmation checkbox and click "Edit Settings". Do not make any changes in this screen, just tell me which is the parent and which is the child.
- If the posts are already in a relationship, how are you trying to connect them again? I'm not clear what you are trying to achieve.
I have a post type, COURSES.
I have a post type, VENUES that is connected to COURSES by relationship.
The relationship is one-to-many, Parent is COURSES, Child is VENUES.
Example:
Course A, Venue 1, 2 & 3
Course B, Venue 4, 5 & 6
I am trying to create a REGISTRATION form to allow my users to register for a course.
The form has a COURSE and a VENUE field that is connected by relationship.
Currently, when a user selects Course A or Course B, it will show all the venues, 1 to 6.
What is wish to achieve is:
If user select Course A, VENUE field should show option Venue 4, 5 & 6.
If user select Course B, VENUE field should show option Venue 1, 2 & 3.
Okay thank you for the explanation. What type of Form is the registration Form? Does it create a new post? Does it edit an existing post? Does it create or edit a User post?
Hi Christian,
this is a post form that creates a new private post.
So each time a user registers, we will know what is the COURSE and what is the VENUE.
Okay there is nothing exactly like this built-in to Forms. Normally if you want to create a relationship between this private post and a Venue, your site visitors would select a Venue from a full list of Venues. There is not a parent select field available in Forms like this. If you want to set this up, it will require a bit of work. You would have to create one generic select field for Courses and also one generic select field for each group of Venues (per Course). You could use Views to populate the options for each select field if you have a large number of options. Then you must use Forms conditional groups to show or hide the Venues select fields based on the selected Course. It's not simple to set up right.
In the cred_save_data hook, you could use the Post Relationships API to link the Venue and the new post. The generic field approach does not automatically link the two posts, so the Post Relationships API is required.
So first I would try to set up the generic select field for Courses. If you have more than a few Courses, it will make sense to use a View. Create a View of Courses. In the Loop Output editor, click "Loop Wizard" and choose "List with separators". Insert the post title and the system will generate a loop for you. Replace the loop contents with this:
<wpv-loop>
[wpv-item index=1]
{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
[wpv-item index=other]
,{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>
Now in the generic select field, you should paste the View in the "options" attribute:
[cred_generic_field field="your-generic-courses-slug" type="select" class="" urlparam=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"",
"options":[[wpv-view name="your-course-list-view"]]
}
[/cred_generic_field]
This should create a select field with one option for each Course. Let me know if you are able to get this far and we can continue from here.
Hi Christian,
followed your instructions.
Now, when I select Course A, Venue 1, 2 & 3 are showing.
When I select Course B, Venue 4, 5 & 6 are showing.
What are the next steps?
Now do you want to use post relationships to link the private post and the Course, or link the private post and the Venue?
Or do you want to add the course and venue values to custom fields in the private post, or the User profile? What is the end goal?
Thank you for the speedy response.
I would like to link the private post and the venue.
Okay so you need a cred_save_data hook to trigger a toolset_connect_posts API call, which will link the two posts.
add_action('cred_save_data', 'connect_venue_to_post',10,2);
function connect_venue_to_post($post_id, $form_data) {
$forms = array( 12345 );
if ( in_array( $form_data['id'], $forms ) )
{
// get the selected Venue ID
$venue_id = $_POST['venue-field-slug'];
// connect the venue to the post that was just created
toolset_connect_posts( 'venue-post-relationship-slug', $venue_id, $post_id );
}
}
You should change 12345 to your Form's ID, then change venue-field-slug to match the slug of your venue field (all the venue fields should have the same slug). Then change venue-post-relationship-slug to match your venue / private post post relationship slug. If the venue is the child in this relationship, you should switch $venue_id and $post_id so the $post_id is the parent.
Great Christian! Perfect! Appreciate all the help!!!!
You're welcome, glad to help.