Latest Tech and Tips

Auto-Redirect when WordPress Search Query Only Returns One Match


There are times when looking a WordPress blog site, you only obtain one thing in the outcome. Depending on exactly how orderly your website is, this need to be the thing the individual was seeking anyways. One of our individuals asked if there was a method to reroute to the message if the search query results just one match. In this write-up, we are mosting likely to reveal you exactly how to reroute individuals to the message when the search query only returns one match.

All you need to do is open your motif’s functions.php documents and also paste the complying with fragment.

add_action(' template_redirect', 'one_match_redirect');.
feature one_match_redirect() {
if (is_search()) {
worldwide $wp_query;.
if ($ wp_query->> post_count == 1) {
wp_redirect( get_permalink( $wp_query->> blog posts['0']->> ID) );.

Now you need to be careful that some individuals will certainly not anticipate this performance. So it might freak them out.