Tuesday, December 1, 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 a WordPress weblog, you solely get one merchandise within the consequence. Relying on how organized your web site is, this must be the merchandise the person was in search of in any case. One among our customers requested if there was a technique to redirect to the submit if the search question outcomes just one match. On this article, we’re going to present you the way to redirect customers to the submit when the search question solely returns one match.

All it’s a must 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 you need to beware that some customers is not going to anticipate this performance. So it could freak them out.

You May Also Like : How to Add an Admin User to the WordPress Database via MySQL

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

How to make money with Toluna surveys

Toluna is most well-known for its surveys, however there are an entire host of different methods to earn rewards on the location. Our assessment...

6 Best Free Alternatives To Camtasia 2020 For Mac And Windows

I really like Camtasia and have been an avid person of Camtasia ever since 2012. Recently, the Camtasia workforce launched the 2019 model of Camtasia which...

List of Travel Affiliate Programs For Travel Bloggers

For me, touring is freedom. If you're a journey blogger, I offer you a standing ovation. Being a digital nomad, I perceive how powerful it's to weblog whereas touring. I additionally...

Social Warfare Review: Explode your Social Shares

Everybody is aware of that having social media sharing buttons is important for each weblog within the Blogosphere. Why? Well, social media sharing buttons assist us...

Recent Comments