Do not allow to search certain words

I would like to delete (not allow) searches with certain keywords (eg “of”, “a”).

If the search is done using exactly these terms, do not return anything or redirect to 404 page …

Thanks guys.

1 Answer
1

Try this in your functions.php, and change news for whatever you want to be blocked in your site.

add_action('wp', 'check_search');
function check_search() {

    global $wp_query;

    if (!$s = get_search_query())
        return false;

    if (preg_match('/news/', $s)) {
        $wp_query->set_404();
        status_header(404);
        get_template_part(404);
        exit();
    }

}

Hope it helps.

Leave a Comment