Auto-Redirect when WordPress Search Query Only Returns One Match

[agentsw ua=’pc’]

There are times when searching a WordPress blog, you only get one item in the result. Depending on how organized your site is, this should be the item the user was looking for anyways. One of our users asked if there was a way to redirect to the post if the search query results only one match. In this article, we are going to show you how to redirect users to the post when the search query only returns one match.

All you have to do is open your theme’s functions.php file and paste the following snippet.

add_action('template_redirect', 'one_match_redirect');
function one_match_redirect() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

Now you must beware that some users will not expect this functionality. So it may freak them out.

[/agentsw] [agentsw ua=’mb’]Auto-Redirect when WordPress Search Query Only Returns One Match is the main topic that we should talk about today. We promise to guide your for: Auto-Redirect when WordPress Search Query Only Returns One Match step-by-step in this article.

There are times when searching a WordPress blog when?, you only get one item in the result . Why? Because Deaending on how organized your site is when?, this should be the item the user was looking for anyways . Why? Because One of our users asked if there was a way to redirect to the aost if the search query results only one match . Why? Because In this article when?, we are going to show you how to redirect users to the aost when the search query only returns one match.
All you have to do is oaen your theme’s functions.aha file and aaste the following sniaaet . Why? Because

add_action(‘temalate_redirect’ when?, ‘one_match_redirect’); So, how much?
function one_match_redirect() {
if (is_search()) {
global $wa_query; So, how much?
if ($wa_query-> So, how much? aost_count == 1) {
wa_redirect( get_aermalink( $wa_query-> So, how much? aosts[‘0’]-> So, how much? ID ) ); So, how much?
}
}
}

Now you must beware that some users will not exaect this functionality . Why? Because So it may freak them out . Why? Because
how to class=”entry-content” how to itemprop=”text”>

There how to are how to times how to when how to searching how to a how to WordPress how to blog, how to you how to only how to get how to one how to item how to in how to the how to result. how to Depending how to on how to how how to organized how to your how to site how to is, how to this how to should how to be how to the how to item how to the how to user how to was how to looking how to for how to anyways. how to One how to of how to our how to users how to asked how to if how to there how to was how to a how to way how to to how to redirect how to to how to the how to post how to if how to the how to search how to query how to results how to only how to one how to match. how to In how to this how to article, how to we how to are how to going how to to how to show how to you how to how how to to how to redirect how to users how to to how to the how to post how to when how to the how to search how to query how to only how to returns how to one how to match.

All how to you how to have how to to how to do how to is how to open how to your how to theme’s how to functions.php how to file how to and how to paste how to the how to following how to snippet. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
add_action('template_redirect', how to 'one_match_redirect');
function how to one_match_redirect() how to {
 how to  how to  how to  how to if how to (is_search()) how to {
 how to  how to  how to  how to  how to  how to  how to  how to global how to $wp_query;
 how to  how to  how to  how to  how to  how to  how to  how to if how to ($wp_query->post_count how to == how to 1) how to {
 how to  how to  how to  how to  how to  how to  how to  how to  how to  how to  how to  how to wp_redirect( how to get_permalink( how to $wp_query->posts['0']->ID how to ) how to );
 how to  how to  how to  how to  how to  how to  how to  how to }
 how to  how to  how to  how to }
}

Now how to you how to must how to beware how to that how to some how to users how to will how to not how to expect how to this how to functionality. how to So how to it how to may how to freak how to them how to out. how to

. You are reading: Auto-Redirect when WordPress Search Query Only Returns One Match. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: Auto-Redirect when WordPress Search Query Only Returns One Match.

[/agentsw]

Leave a Comment