To do it in query handling stage, may use request hook which is when WordPress query is being setup
.
The term link is recommended by @Jacob Peattie using built-in get_term_link()
instead of building it manually for more flexibility and error-proof.
add_filter( 'request', 'ws366006_check_request_query' );
function ws366006_check_request_query( $query ) {
// var_dump($query);
// redirect checking
if( ! empty( $query['s'] ) ) {
$search = $query['s'];
if ( term_exists( $search, 'post_tag' ) ) {
$url = get_term_link( $search, 'post_tag' );
wp_safe_redirect( $url );
exit();
}
}
// normal return
return $query;
}
If want to use custom query parameters that is not publicly available in WordPress, just add it to the allowed list by query_vars hook.
add_filter('query_vars', 'ws366006_add_query_vars' );
function ws366006_add_query_vars( $query_var ) {
$query_var[] = 'searchQuery';
return $query_var;
}