Thursday, December 3, 2020
Home Blogging Auto-Redirect when WordPress Search Query Only Returns One Match

Auto-Redirect when WordPress Search Query Only Returns One Match

There are occasions when looking out a WordPress weblog, you solely get one merchandise within the outcome. Relying on how organized your web site is, this needs to be the merchandise the person was searching for in any case. One in every of our customers requested if there was a strategy to redirect to the publish if the search question outcomes just one match. On this article, we’re going to present you tips on how to redirect customers to the publish when the search question solely returns one match.

All it’s important to do is open your theme’s capabilities.php file and paste the next snippet.

1
2
3
4
5
6
7
8
9
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 it’s essential to beware that some customers is not going to count on this performance. So it might freak them out.

You May Also Like : How to Create Advanced Search Form in WordPress for Custom Post Types

If you liked this article, make sure to hit the bell for getting our latest updates and share this article with your friends in every social media platforms.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

LIC Premium Paid Certificate Download For Income Tax Purpose – Online

You should be conscious that it's necessary to provide Premium Paid Certificate for insurance coverage insurance policies to avail Income tax exemption profit underneath...

Leave Encashment Calculation & Tax Implications

Leave Encashment is a course of the place you may get cash for the unused stability leaves. Most of the persons are not conscious...

Rakesh Jhunjhunwala Portfolio Holdings

Rakesh Jhunjhunwala, 60, India’s most profitable fairness investor who made 2.9 Billion {dollars} (290 crores USD) ranging from $100. Rakesh Jhunjhunwala is a clever man with...

Angel Broking Vs Zerodha Broker Comparison : Brokerage, Margin, Charges

I've carried out the comparability between Angel Broking vs Zerodha (a reduction dealer) on this article. If you might be on the lookout for advisory dealer...

Recent Comments