Skip Navigation

[Resolved] Display conditionally options on a Toolset form relationship select

This support ticket is created 5 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 8 replies, has 2 voices.

Last updated by Nigel 5 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1354243

Hi I've 4 CPTs, they are connected by relationships. I've created a form on the single page of CPT1 where I want to create a new Post of CPT4, that is child of 1, 2 and 3. So on the form I have 3 selects to choose the 2 parents.

What I need is to display on the selects only certain possible parents depending on the page where I'm. I'm on a single page of CPT1 that is parent of cartain CPT2 and CPT3 posts, so I want to display only those posts that are relatives of the page where I am.

Is there a way to achive this? Is there a way to filter options on a relationship select by any relationship criteria?

Is there a way to deactivate select2?

thanks

#1354631

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The only customisation of the parent select field possible is to limit the parents to those authored by the current user, so someone adding child posts only sees their own parent posts as options.

There aren't any other possibilities to customise the parent selector.

If you wanted to do something different than the default behaviour you would need to exclude the parent select fields from the form and construct your own generic inputs that list the limited parent choices according to your needs, and then add your own form submission handling to connect the respective posts.

Depending on what these conditions are for the parent selectors, you may be able to make Views to output the options in JSON format to provide the select options.

You would use the cred_save_data API hook to run some code that took the values for the parents and used toolset_connect_posts to establish the connections. (See https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts)

#1358115

Hi Nigel, I'm working with API as you suggested. I'm using toolset_connect_posts( 'book-author', 5, 7 ); example to connect programmatically posts, by one-to-many and many-to-many relationships.

Now I need to let the user to choose from a multiselect more then one Posts to connect to a specific post. How can I do it programmatically?

thanks

#1358719

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I'm not sure if you are asking me about displaying a multiselect field in the form, or processing it after the form has been submitted.

You can insert a generic multiselect field into the form and have the options come from a View which generates the required JSON output.

When it comes to processing the form submission, if you include a generic multiselect field in the form then it will be available in the $_POST object with the slug of the generic field as an array of the selected options, e.g.

   [selections] => Array
        (
            [0] => 1
            [1] => 3
        )

is from the $_POST object for a field "selections" where I had selected the options with values 1 and 3.

You have to connect each post individually with toolset_connect_posts, meaning that you would use a foreach loop to iterate over the array of selected options and connect each of them one at a time.

Is that the kind of thing you meant?

#1358723

Yes, thanks this was what I need.

Can you show me an example of the foreach loop?

thanks

#1358727

Should this work fine?

$child_id = $_POST['child-id']; 

foreach($child_id as $value=>$id) {
		toolset_connect_posts( 'supplement-side-effect', $parent_id, $id );
	}

where child-id is the slug of the generic field

#1358781

My issue is resolved now. My PHP code works fine.

Thank you!

#1358801

Hey, I still have an issue. I'd like to get the ID of a new intermediary post created by toolset_connect_posts on cred_save_data.

How can I do it? I found toolset_association_created, but I'm not sure how can I use it.

After I get the ID, I'd like to store it in a custom field of another post.

thanks

#1358823

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Check the documentation for toolset_connect_posts again: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

It says it returns an array that includes the element 'intermediary_post' which is the ID of the created intermediary post when relevant.