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

Reindirizzamento automatico quando la query di ricerca di WordPress effettua una sola corrispondenza

Nota editoriale: guadagniamo una commissione dai link dei partner su WPBeginner. Le commissioni non influenzano le opinioni o le valutazioni dei nostri redattori. Per saperne di più su Processo editoriale.

A volte, quando si effettua una ricerca su un blog WordPress, si ottiene un solo elemento nei risultati. A seconda dell’organizzazione del sito, questo dovrebbe essere comunque l’elemento che l’utente stava cercando. Un utente ci ha chiesto se c’era un modo per reindirizzare all’articolo se i risultati della ricerca effettuano una sola corrispondenza. In questo articolo vi mostreremo come reindirizzare gli utenti all’articolo quando la query di ricerca effettua una sola corrispondenza.

Tutto ciò che dovete fare è aprire il file functions.php del vostro tema e incollare il seguente frammento.

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

Ora bisogna fare attenzione al fatto che alcuni utenti non si aspettano questa funzionalità. Quindi potrebbe spaventarli.

Divulgazione: I nostri contenuti sono sostenuti dai lettori. Ciò significa che se cliccate su alcuni dei nostri link, potremmo guadagnare una commissione. Vedi come WPBeginner è finanziato , perché è importante e come puoi sostenerci. Ecco il nostro processo editoriale .

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.

Il kit di strumenti WordPress definitivo

Ottenete l'accesso gratuito al nostro kit di strumenti - una raccolta di prodotti e risorse relative a WordPress che ogni professionista dovrebbe avere!

Reader Interactions

7 commentiLascia una risposta

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Casey Lessard says

    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!

  3. Eric says

    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.

  4. Nebulas Website Design says

    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?

  5. carlos says

    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.

Lascia una risposta

Grazie per aver scelto di lasciare un commento. Tenga presente che tutti i commenti sono moderati in base alle nostre politica dei commenti e il suo indirizzo e-mail NON sarà pubblicato. Si prega di NON utilizzare parole chiave nel campo del nome. Avremo una conversazione personale e significativa.