Skip Navigation

[Resolved] filter post relationship dropdown by taxonomy

This thread is resolved. Here is a description of the problem and solution.

Problem:
filter post relationship dropdown by taxonomy

Solution:
You need to use "pre_get_posts" action filter to filter the parent posts that is displayed using the parent field on toolset form.

You can find the proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/filter-post-relationship-dropdown-by-taxonomy/#post-1220408

Relevant Documentation:

100% of people find this useful.

This support ticket is created 5 years, 8 months 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
- 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 6 replies, has 2 voices.

Last updated by Mukesh 5 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1219342

Tell us what you are trying to do?
I have a post form for a CPT Referral where I've added a dropdown for a post-relationship (post reference for another CPT called Clinics) field. The Clinics CPT has a taxonomy field where a Clinic can be a medical clinic or a chiropractor clinic. On the Referral Post form, the dropdown shows all the values/posts I've created for Clinics but I would like to filter that dropdown options by the taxonomy.

Is there any documentation that you are following?
I saw this post: https://toolset.com/forums/topic/filtering-options-by-taxonomy-when-setting-up-post-relationships/
where the customer had same request. Then I researched and found a API Filter cred_filter_field_before_add_to_form and I am wondering if that filter can be used to add my own dropdown options? Can you please provide some guidance?
Is there a similar example that we can see?
https://toolset.com/forums/topic/filtering-options-by-taxonomy-when-setting-up-post-relationships/
What is the link to your site?

#1219431

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - as such there is no such feature exists to filter the post-relationship drop-down with taxonomy.

You should try to use the workaround shared by the Luo with the following reply:
=> https://toolset.com/forums/topic/filtering-options-by-taxonomy-when-setting-up-post-relationships/#post-915669

In addition to this, I think this is a useful feature, please feel free to submit the feature request using the following link:
=> https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

#1219689

Thanks Minesh. The problem I see with the solution by Luo is that I already have a this application in production and there are thousands of referrals already made with the post-relationship dropdown, so changing to a generic field will cause problems.

So the hook 'cred_filter_field_before_add_to_form' can't be used to modify the dropdown options before displaying?

#1220408

Minesh
Supporter

Languages: English (English )

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

Well - what if you try to add the following code and check if that help you to resolve your issue.

function filter_container_archive( $query ){
 global $post;
 if (defined('DOING_AJAX') && DOING_AJAX && $post->ID==9999) { 
    
	if($_REQUEST['action']=='select2_potential_relationship_parents'){
		
		if($query->query['post_type'][0]=='student'){
			 $taxquery = array(   array(
                                 'taxonomy' => 'employee-department',
                                 'field' => 'slug',
                                'terms' => array('dept-account'),
                                'operator'=> 'IN' ));

			$query->set( 'tax_query', $taxquery );
			
		}
		
	}
	
	
 }
   
}
add_action( 'pre_get_posts', 'filter_container_archive', 101, 1 );

Where:
- Replace 9999 with the post/page ID where you added the form
- Replace student with your parent post type slug
- Replace employee-department with your taxonomy slug
- Replace dept-account with your taxonomy term slug

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

#1220607

Hi Minesh,

This code worked nicely. I had to refactor a little to fit my exact scenario for example on my Form I have two dropdowns; one showing filter list by one term and another filtering by another term of the same taxonomy. So in addition to checking for $_REQUEST['action'] I also check if the $_REQUEST['slug'] is also present with the value the field slug. And I add the relevant term to the $taxquery. I also had to check if the $post was an object because on loading of the form, there were multiple calls to the backend via ajax and some didn't have the $post. Not sure if that is just my specific situation. Other than that rest of the code worked fine.
Thanks again for a quick and valuable solution!

#1220608

Minesh
Supporter

Languages: English (English )

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

I am really glad to help and thank you for your confirmation that solution I shared help you to resolve your issue.

Please resole this ticket and feel free to share final solution here that may help other users searching on the forum.

#1220629

My issue is resolved now. Thank you! Here is the code I used which was a slight modification to Minesh, just so it fit my example:

function filter_container_archive( $query ){
    global $post;
   $post_id=default_value; //on occasion the $post will be null and so we can set a default value
//check if the $post is not empty and is an Object
    if ( !empty($post) && is_a($post, 'WP_Post') ) {
        $post_id=$post->ID;
    }
    if (defined('DOING_AJAX') && DOING_AJAX && $post_id==20) { 
       //check for the action & slug to focus in on a specific dropdown if you have multiple dropdown in the form
       if($_REQUEST['action']=='select2_potential_relationship_parents' && $_REQUEST['slug']=='slug_of_field'){
            if($query->query['post_type'][0]=='post_type_of_parent'){
                 $taxquery = array(   array(
                                     'taxonomy' => 'taxonomy',
                                     'field' => 'slug',
                                    'terms' => array('taxonomy_term_value'),
                                    'operator'=> 'IN' ));
                $query->set( 'tax_query', $taxquery );
            }
        }       
    }

}
   add_action( 'pre_get_posts', 'filter_container_archive', 101, 1 );