• Resolved mikeecho

    (@mikeecho)


    Is there anyway to stop user from clicking “Search” with no search text having being entered, as this seems to break my site and the plugin, as it then no longer cares about excluded categories or even the fact that it is only suppose to search in POSTS.
    When user enters no text in “Search” field, and clicks Search, it returns results from Posts, Pages, and ALL categories, even though the Plugin Settings only say POSTS

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Ajay

    (@ajay)

    Please can you disable the plugin and tell me what you see with a blank search term?

    I remember looking into the this and if there is an empty search term, then the plugin doesn’t make any changes to the default search behaviour within WordPress.

    I think this code might help – note it’s outside of Better Search.

    https://grafxflow.co.uk/blog/content-management-systems/prevent-empty-searches-in-wordpress-revised

    Thread Starter mikeecho

    (@mikeecho)

    Hi Ajay
    Yes, I kind of figured that it is not Better Search, but the main WordPress search causing it. So I managed to find some code that stop the main search in it’s tracks, when the search terms passed is blank.
    Here it is, in case someone else has a similar issue:

    add_filter( 'posts_search', function( $search, \WP_Query $q )
    {
        if( ! is_admin() && empty( $search ) && $q->is_search() && $q->is_main_query() )
            $search .=" AND 0=1 ";
    
        return $search;
    }, 10, 2 );
    Plugin Author Ajay

    (@ajay)

    Thanks for this code.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Blank Search term’ is closed to new replies.