Simon Blackbourn

Excluding spammy searches from SearchWP statistics

What is SearchWP?

SearchWP is a fantastic premium WordPress plugin that transforms the default WordPress search. It allows you to order and weight results, define multiple search engines (e.g. for different post types or different sections of your site), search taxonomies and custom fields, and much more. It's fast and highly customisable. The support is also top notch - the best I've received for any WordPress plugin.

One of its best features is the logging and statistics, which give you a vital insight into what your visitors are searching for, so you can tailor your content accordingly.

Spam searches

However, I've recently noticed that more and more spambots are crawling sites and searching for annoying, spammy, and very often offensive, search terms. This doesn't cause any damage to your website, but it does mean that you have to see all these search terms when you view the stats, which skews the stats and makes finding the genuine search terms amongst them quite difficult.

Here's a screenshot of the stats page for one of my clients' sites where things had gotten quite out of hand!

A screenshot showing lots of spammy searches on a WordPress website

Luckily there's a straightforward way to exclude all this garbage from being logged...

How to fix it

Add the following code in the functions.php file in your theme, or make a little plugin for it:

add_filter( 'searchwp\statistics\log', 'my_log_filter', 10, 2 );

function my_log_filter( $enabled, $query ) {

    $search_string = $query->get_keywords();
    $long          = ( strlen( $search_string ) > 30 );
    $www           = ( 'www' === substr( $search_string, 0, 3 ) );
    $slashes       = ( false !== strpos( $search_string, '/' ) );

    if ( $long or $www or $slashes ) {
        return false;

    return true;


View this code on Gist

This prevents any search from being logged that meets any of the following conditions:

It's more than 30 characters long,
It starts with 'www',
It contains a forward slash.

I've found this has completely removed all spambot searches from the logs, and hasn't removed any legitimate searches.

Of course, your needs may be slightly different, so customise it as needed.

Note: this only works for version 4.x of SearchWP (if you're running version 3.x I'd highly recommend you upgrade)

The result

A nice clean statistics screen :)

A screenshot showing the SearchWP statistics screen