Skip Navigation

[Resolved] Form with related child

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

Author
Posts
#2683346

Hi

I'm building a contact list.
I have a CPT "Companies" and a "Contacts" with a relation One (Company) to Many (Contacts).
"Companies" have a CF "type of company" (producer, distributor, client)
I have another CPT "Product" with a relation One (company) to many (products)

I created a form to add a new product.
In that form I can add the related company to the product, but is there a way to being able to select only the Companies with the "type of company"="producer" ?

#2683545

Hi,

Thank you for contacting us and I'd be happy to assist.

A new filter 'types_relationship_connect_existing_options' was recently introduced which can be used to filter the options shown in the post-relationship field, when connecting posts:
https://toolset.com/documentation/programmer-reference/types-api-filters/#types_relationship_connect_existing_options

In your custom code, you can use either a custom query or a Toolset classic view to first get the list of 'Company' posts with a 'producer' value stored in the custom field and then pass those posts as options, through this filter.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2683581

Hi Waqar.

Thanks for the infos.
Can you help me on how to make that limited list ? How should I format my simple View ? How can I integrate it to my custom code ?

#2683762

I can help you with some code examples, but I'll need to understand the setup of these post types and forms first.

Can you please share the temporary admin login details of the website, along with the link to the page where this relationship form can be seen?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2683764

Thanks a lot Waqar.

Unfortunately I'm working with. Mama Pro in local...
How can we proceed ?

#2683809

To restart the explanation more adapted to my project. I have 2 CPT's : 'products' and 'companies'
The CPT 'companies' has a few custom fields. One of the field is called "company-status" and is a "Checkboxes" selection.
3 status (for the moment) in this selection : producer, licensor and distributor.

I define a 1 (Company) to Many (products) relationship that I called 'licensor-of-the-product'.

I created a form to add a new 'Product'. One of the form field is the relationship field 'licensor-of-the-product'.
This is where I need to only display the companies that have at least the 'licensor' option checked in its 'company-status' field.

This is the script I wrote but it still display all the companies:

add_filter('types_relationship_connect_existing_options', function($options, $other_post_type, $relationship_slug, $current_post_id) {
if ('licensor-of-the-product' === $relationship_slug && 'company' ===$other_post_type)
{
$licensor_companies = get_posts(array(
'post_type' => 'companies',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'company-status',
'value' => 'licensor',
'compare' => 'LIKE',
),
),
'fields' => 'ids',
));

$options = array();

foreach ($licensor_companies as $company_id) {
$options[] = array(
'label' => get_the_title($company_id),
'value' => $company_id,
);
}
}

return $options;
}, 10, 4);

Unfortunately when I create a new Product it still displays all the companies.
I'd like also this script working for a new Product or when Edit a Product.

Can you help me?

#2684080

Thank you for sharing these details.

I've performed some tests on my website and can confirm that the filter 'types_relationship_connect_existing_options' works for the relationship field on the post-edit screen in the admin area, but not for the front-end form.

For the front-end forms, you'll need to use a different workaround:

1. In your form to add a new product, you can replace the default relationship selection field for the 'company' with a 'select' type generic field.

2. This generic field will be set to populate the option through a custom shortcode, that will generate the options from the post type 'company' with necessary filtering, based on the custom field.

3. The last step would be to use a custom function attached to the 'cred_save_data' hook to connect the company post selected through the generic field with the newly created product post when the form is submitted.

All these steps along with the example code snippets are available in the following forum reply:
https://toolset.com/forums/topic/adjust-content-of-relationship-field-in-cred-form/#post-2379437

I hope this helps and please let me know if you need further assistance.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.