• Resolved steveraven

    (@steveraven)


    Hi guys,

    About a year ago I asked if it were possible to add a code snippet that would redirect users from the ‘no search results found’ page, to a custom page that showed videos of cats.

    Just thought I’d retry it out last night, and it’s no longer working.

    The code snippet supplied was:

    /*Relevanssi Snippet */
    add_filter( 'relevanssi_modify_wp_query', 'rlv_empty_redirect' );
    function rlv_empty_redirect( $query ) {
        if ( empty( $query->query_vars['s'] ) ) {
            wp_safe_redirect( '/no-results/' );
            exit();
        }
        return $query;
    }

    …and I wondered if over the last year with all the WordPress updates, it no longer worked?

    Thanks for any help!

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

    (@msaari)

    Hmm, actually, it’s better not to combine the two. It’s much better to run the empty query string check before the search, and you can only run the empty results check after the search. So, two separate functions is the way to go:

    add_filter( 'relevanssi_hits_filter', 'rlv_no_results_redirect' );
    function rlv_no_results_redirect( $hits ) {
        if ( count( $hits[0] ) < 1 ) {
            wp_safe_redirect( '/no-results/' );
            exit();
        }
        return $hits;
    }
    
    add_filter( 'relevanssi_modify_wp_query', 'rlv_no_query_redirect' );
    function rlv_no_query_redirect( $query ) {
        if ( empty( $query->query_vars['s'] ) ) {
            wp_safe_redirect( '/no-results/' );
            exit();
        }
        return $query;
    }
    Thread Starter steveraven

    (@steveraven)

    That set of snippets ticks all the boxes – thank you so much for sorting my invalid search terms out!

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘Custom ‘No Results’ Page Not Working’ is closed to new replies.