Filter the Parent Post Selector Select2 Input in Toolset Forms by whatever you want

Sometimes it’s required to filter the Parent Post Selector by more than just the current user as author.

For example you may want to filter the posts in the Select2 by a specific taxonomy term and show only those with term “my term” in the Select2 so when users connect posts with the form, they can only see the potential parent posts with that term set.

This is a sample code that should help to work further with such customizations.

function ts_custom_func( $query ){

	
//If you have many forms on the page do whatever you can, I am still trying to find the code I used to check the cred_form_id_on_page or soo...

    if (defined('DOING_AJAX')) { //We only want to run this if an Actual AJAX Request happens


       if($_REQUEST['action']=='select2_potential_relationship_parents'){ ////We only want to run this if an Actual AJAX Request to the actual select2_potential_relationship_parents action happens

            if($query->query['post_type'][0]=='page'){ //if query is for post type and of type PARENT_POST_TYPE

            	//This is just an example but works
                $taxquery = array(   array(
                                    'taxonomy' => 'mytax-slug',
                                    'field' => 'slug',//this just defineds the type of field you query, not the Types Term fields!!!
                                    'terms' => array('a-term-slug'),//Since we say slug we use slug
                                    'operator'=> 'IN' ));

                $query->set( 'tax_query', $taxquery );

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

You can find this and other solutions as well here:
https://toolset.com/forums/topic/how-to-show-in-cred-parent-selector-only-posts-of-logged-in-user/page/2/#post-605405
https://toolset.com/forums/topic/filter-post-relationship-dropdown-by-taxonomy/#post-1220408


Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments