Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Pierre Brouyaux

Filtering taxonomy results (Select2 ajax list)

Hello,

I have a “taxonomy” column where the user can use inline-editing to modify tags of a post.

I would like to limit the list of tag choices according to special criteria. I mean, the list displayed by the “Select2” module, which is loaded by Ajax.

Despite a lot of research, I can’t find a solution. WordPress has a hook on “get_terms”, but the hook has no effect on the Ajax list. Actually, I’ve tried all possible hooks, none of which seem to affect the results of this query.

Do you think there is a solution?

Have a nice day

2 years, 10 months ago
Pierre Brouyaux

I found the beginning of a solution. I create my own hook to answer the Ajax request, with a higher priority.

I still need to improve the Json response. And find a solution to check the nonce, otherwise I have a security problem.

add_action( 'wp_ajax_acp_editing_single_request', 'ajax_response_taxonomy_column_editing', 1 );

function pbx_ajax_response_taxonomy_column_editing() {

    if( empty( $_GET['column'] ) ) {
        return;
    }

    if( 'COLUMN_ID' !== $_GET['column'] ) {
        return;
    }

    // Create de $terms_response array

    $response = array(
        'success' => true, 
        'data' => array( 
            'results'   => $terms_response
        )
    );

    wp_send_json( $response, 200 );
    
    wp_die();
}
2 years, 10 months ago
Stefan van den Dungen Gronovius
Developer

The select2 search query goes through the WP_Term_Query by a specific Ajax call for our plugin.
If you want to change the arguments for the query, you could combine the information about to write a hook like this:

add_filter( 'get_terms_args', function( $args ){
	if( 'acp_editing_single_request' === filter_input( INPUT_GET, 'action' ) ){
		// Alter arguments for the main query
		$args['search'] = 'CSS';
	}
	
	return $args;
});

If you want to change anything else from the WP_Term_Query you can use the needed hook with the simple action check to make sure you’re targeting the query fired on our Ajax call.

2 years, 10 months ago
Pierre Brouyaux

Thanks Stefan,

It works perfectly.

2 years, 10 months ago

You must be logged in to reply to this topic.