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