• Resolved Tobias

    (@tobiaseichner)


    Hi,

    I have the following application to set up (beside Relevanssi, I also use its live-ajax-search companion plugin):

    If a user searches for a term for which there is no match, a predefined post should be called up. I found a code snippet in the Relevanssi documentation, which I have adapted (functions.php):

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

    Unfortunately, just an empty screen is returned instead of the post (ID exists).

    What have I done wrong? Maybe a theme-related issue?

    Also I am looking for a solution that instead of “Sorry, no results found” during the live search, an entry “Show information about XXX” should be shown that is linked up to the specific post ID (XXX replaces the search term obviously).

    Any hint is highly appreciated.
    Tobias

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

    (@msaari)

    In the function, you’re checking that $wp_query->posts is empty, yet you’re referring to a post in the array ($wp_query->posts['1395']->ID). If you want the permalink to post 1395, it’s just get_permalink( 1395 ).

    You can modify the live search results by creating a custom template for the search results. See the instructions here.

    Thread Starter Tobias

    (@tobiaseichner)

    Thank you! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redirect to specific post if no search result is found’ is closed to new replies.