• 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 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Mikko Saari

    (@msaari)

    I just checked this, and the code works, no problems there.

    However, the code does not even try to redirect users from an empty results page – it redirects searches where no search term is specified. If there’s a search term and no results found, the redirect won’t trigger.

    This code redirects when there are no results found:

    add_filter( 'relevanssi_hits_filter', 'rlv_empty_redirect' );
    function rlv_empty_redirect( $hits ) {
        if ( count( $hits[0] < 1 ) ) {
            wp_safe_redirect( '/no-results/' );
            exit();
        }
        return $hits;
    }
    Thread Starter steveraven

    (@steveraven)

    Hang on Mikko, I’m getting a ‘temporarily unavailable’ error so I reckon it’s in the code placement or something.

    Just trying to get in touch with my host now…

    Thread Starter steveraven

    (@steveraven)

    Right, I’ve just come from my host, and the ‘technical difficulties’ message from before is being generated by WordPress…

    The issue is that filter breaks the WP process as WP is the one generating that message.

    …so there’s something in my website that is conflicting with that filter I reckon.

    Plugin Author Mikko Saari

    (@msaari)

    If you have your function and my function at the same time, that’s not going to work, because they have the same name. You have to rename one of them.

    Thread Starter steveraven

    (@steveraven)

    No, they were both tried independently, and each one threw up the ‘technical difficulties’ error.

    I’ve also tried adding it through the ‘Code Snippets’ plugin, which needed deactivating via cpanel to get my website working again.

    For info, the function for the first snippet of code was ‘Relevanssi Snippet’, and we named the new piece of code ‘Relevanssi Hits Snippet’.

    In any occasion, the first snippet worked fine when it was last checked a few months ago.

    Plugin Author Mikko Saari

    (@msaari)

    The first snippet still works perfectly fine and redirects when it’s given an empty search query. The code I gave was a direct copy/paste from my test site, where it works and redirects empty search results as expected.

    That kind of “technical difficulties” message can mean many things. I can help if you can provide the actual error message; if this is caught by the WordPress protection, doesn’t it report the error message in an email?

    But it’s possible it’s something really small, like the apostrophes being changed to single curly quotes or something like that, or an invisible control character at some point instead of a space. The code itself should work, I just tried copying it from the page and I get no errors.

    Thread Starter steveraven

    (@steveraven)

    No, there are no email messages being sent, and the error message is just a huge white screen, which just reads ‘this site is experiencing technical difficulties’, no more, no less.

    And then there’s the problem with the ‘code snippets’ plugin, which I always used to use way back when.

    When trying this method, I need to access cpanel to deactivate it, suggesting that something is wrong somewhere.

    Thread Starter steveraven

    (@steveraven)

    Right then Mikko,

    Major apologies needed as I’ve just been into functions.php from my cpanel, and the code is there – meaning that it was either left there from before, or placed in there by my host yesterday – judging by the ‘tech difficulties’ message my host was getting, I’d suggest the first option.

    Seeing as I’d forgotten that an empty search term box would be the only reason for a No Results page to show, it was my mistake entirely.

    Clicking on an empty search box does indeed take you to the ‘no results’ page as before, but in your second snippet:

    add_filter( 'relevanssi_hits_filter', 'rlv_empty_redirect' );
    function rlv_empty_redirect( $hits ) {
        if ( count( $hits[0] < 1 ) ) {
            wp_safe_redirect( '/no-results/' );
            exit();
        }
        return $hits;
    }

    …would that show the same no results page if someone typed in an unfound search term?

    • This reply was modified 5 years, 3 months ago by steveraven.
    Plugin Author Mikko Saari

    (@msaari)

    Yes, that function triggers whenever there are zero hits found, no matter what the search term is.

    Thread Starter steveraven

    (@steveraven)

    Just replaced the first snippet with the second snippet, and it works superbly – even on an empty search box too!

    Wonderful support, thank you so much!

    Thread Starter steveraven

    (@steveraven)

    Ahh – not quite though.

    It now seems that everything I search for, whether it be a valid search term, or an invalid one, sends the user to the ‘No Results’ page.

    I’ll need to replace the snippet back to version one.

    Plugin Author Mikko Saari

    (@msaari)

    There’s a typo in the code:

    add_filter( 'relevanssi_hits_filter', 'rlv_empty_redirect' );
    function rlv_empty_redirect( $hits ) {
        if ( count( $hits[0] ) < 1 ) {
            wp_safe_redirect( '/no-results/' );
            exit();
        }
        return $hits;
    }
    Thread Starter steveraven

    (@steveraven)

    That works much better now – goes straight to the ‘no results’ page if it’s an invalid search term, so I’ll now use your second snippet.

    Thanks for sorting out the typo!

    The empty search box thing that worked before though, now presumes that you’ve searched for “” and displays a series of pages in the results.

    Is there a way to combine the first, and the second snippet, so that clicking on an empty search box, AND clicking on an invalid search term, sends users to the ‘No Results’ page?

    No matter if this is not possible, as the snippet as-is works excellently!

    • This reply was modified 5 years, 3 months ago by steveraven.
    Plugin Author Mikko Saari

    (@msaari)

    Yes, you can combine both conditions to the same function:

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

    (@steveraven)

    Just tried the code snippet, and it’s sending ALL terms to the ‘No Results’ page again.

    Is there a typo?

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