• Is it possible to hide the search results altogether and redirect to the most relevant result post?

Viewing 1 replies (of 1 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Yes. This should do the trick:

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

    This checks if it’s a search, then if there’s at least one result found, the function will automatically redirect the user to the first result.

Viewing 1 replies (of 1 total)
  • The topic ‘Hide results and got matching post’ is closed to new replies.