Block internal search queries with pre_get_posts and regex rules

This relates to my previous question: Internal search spam (UPD: I no longer use Ivory Search)

The closest question I found: Block search keywords

As I understood, I need to use pre_get_posts, but I don't exactly know where it should go (functions.php?) and how I can create a regex-based rule to exclude everything that has "www." in it and non-Latin or non-numeric symbols like emojis and Cyrillic letters.

I have the results noindexed and blocked in robots.txt, but I'd like to fix the root of the problem if possible by stopping the queries from happening at all. Any help here would be greatly appreciated.

Topic pre-get-posts regex wp-query spam Wordpress search

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.