Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Automatyczne przekierowanie, gdy zapytanie wyszukiwania WordPress zwraca tylko jedno dopasowanie

Zdarza się, że podczas przeszukiwania bloga WordPress w wynikach pojawia się tylko jeden element. W zależności od tego, jak zorganizowana jest twoja witryna, powinien to być element, którego użytkownik i tak szukał. Jeden z naszych użytkowników zapytał, czy istnieje sposób na przekierowanie do wpisu, jeśli zapytanie wyszukiwania daje tylko jedno dopasowanie. W tym artykule pokażemy, jak przekierować użytkowników do wpisu, gdy zapytanie zwróci tylko jedno dopasowanie.

Wszystko, co musisz zrobić, to otworzyć plik functions.php twojego motywu i wkleić następujący fragment kodu.

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 ) );
        }
    }
}

.

Teraz musisz uważać, że niektórzy użytkownicy nie będą oczekiwać tej funkcjonalności. Więc może ich to przerazić.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

6 komentarzyLeave a Reply

  1. Casey Lessard

    Worked for me. I run a local search engine using WordPress and I have been trying to find this solution for some time. Does exactly as described. Thank you so much!

  2. Eric

    I want to make it so when some searches a specific phrase it redirects to an easter egg, like if someone searches “I see dead people” specifically it’ll redirect them to a URL I can define.

  3. Nebulas Website Design

    Thanks for this piece of code it made my client very happy and I didn’t thnk it could be done. However it’s not working for us we are using the Relevanssi search plugin should it work with this?

  4. carlos

    What if I want to redirect any search query to the most relevant post no matter what the number of posts the search returns? I’d really like to be able to do that.

  5. CalebZahnd

    This is handy. Thanks!

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.