Skip Navigation

[Resolved] Filtering selectable relationship field based on custom fields

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 0 replies, has 1 voice.

Last updated by fabriceS 1 day, 7 hours ago.

Assisted by: Minesh.

Author
Posts
#2794177

Hi

In a form with a relationship field, I'd like to filter the source of the relationship field items.

Let's say I have 2 CPT: DELIVERIES and COMPANIES with a relationship 1 (company) to many (deliveries).

I the form to create a new delivery, I add the selectable field to link it to a COMPANY.

This is showing a dynamic selectable field in the form page that will list all the registered companies.

This COMPANY CPT have a multiple checkboxes field called 'type' with 3 checkboxes options : client, provider, delivery.

I'd like for my "new delivery" form the relationship selectable field of "company" show only the companies that have a custom field type=client.

How can I achieve that ?

#2794185

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

It seems we can filter the result of parent dropdown selector in one-to-many relationship by adding hook/filters but as you are using teh checkboxes field let me check your current settings and get back to you with possible solution.

Can you please share problem URL where you added the form as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2794582

Minesh
Supporter

Languages: English (English )

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

Can you please try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

Alternatively - you can also add the following code to your current theme's functions.php file:

function func_filter_relationship_dropdown_select2_custom_field_checkboxes_option_value($query){
    global $post;
   

    if (defined('DOING_AJAX') && DOING_AJAX and isset($_REQUEST['action']) and isset($_REQUEST['slug']) and isset($_REQUEST['form_type'])	 ) { 
	
		$parent_post_type = 'company';  /// parrent post type slug
		$relationship_slug = 'delivery-company'; ///relationship slug
		
     
       if($_REQUEST['action']=='select2_potential_relationship_parents' and $_REQUEST['slug']==$relationship_slug){
             
            if($query->query['post_type'][0]==$parent_post_type ){
				$meta_query = array(   array(
                                     'key' => 'wpcf-types',
                                     'value' => 'wpcf-fields-checkboxes-option-a262492a679c3e08df1f454ad210d2fd-1',
                                     'type' => 'CHAR',
                                    'compare'=> 'LIKE' )
								);
                $query->set( 'meta_query', $meta_query );
				
				
            }
        }       
    }
}
add_action( 'pre_get_posts', 'func_filter_relationship_dropdown_select2_custom_field_checkboxes_option_value', 101, 1 );

I hope above solution will help you to resolve your issue.

#2794590

Thanks a lot ! It works !