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

Show only certain users posts in “Relationships” field of Toolset Forms.

Toolset Forms allows filtering the Posts shown in the “Relationships” fields where one can pick a related post to the currently edited or created post.
Those filters allow (in the GUI) to return either all authors posts or the current logged in users posts.

This below filter should allow you to return any users post filtered by the (single) ID of a user.
The shortcode of Forms is not able to accept an Array of users, hence you can only pass one user ID each form/field.

function load_specific_users_as_author( $force_author) {
    //Dull example of loading only posts of author with ID 1
    $force_author = 1;
 
    //Or, alter the $force_author as you like, it must return one Users ID. It does not accept arrays.
     
    //Return the $force_author 
    return $force_author;
}
 
//Apply the filter
add_filter( 'cred_force_author_in_related_post', 'load_specific_users_as_author', 10 );

This will filter posts in the selector to only the ones of user ID 1 (or as set by your custom query)

There are 4 more filters you might use, however none including the above are documented, hence this snippet is not public.