WordPress Search and Empty Queries

After many years, while working on a new project, I noticed that searches on WordPress sites with an empty search query return every searchable, published item from the posts table instead of no results.

This doesn’t seem correct—an empty search should return no results.

Doing a web search for the problem, most answers suggest modifying the search query with $query->set('post__in', array(0));. This doesn’t work. The results still contain everything.

The post__in array needs one small change to work; switch the 0 to a -1.

Add the final function to functions.php:

function mysite_empty_search_term_results($query)
{
  $search_query = trim(get_search_query());

  if (!is_admin() && $query->is_main_query() && $query->is_search() && empty($search_query)) {
    $query->set('post__in', array(-1));
  }
}

add_action('pre_get_posts', 'mysite_empty_search_term_results');Code language: PHP (php)

My Brain says Hi!

Psychedelic illustration of the letter H with the letter I nested in the background to say Hi
Digital illustration created in Procreate

More is more when it comes to this illustration. It started as doodling randomly with symmetry enabled in Procreate, and I just kept going. I let my brain dance across the canvas without worrying about what the drawing would be, if I focused too much in one area, skipped around the canvas too much, or even if I’d finish it.

The random and carefree nature of the exercise worked well for my ADHD brain, and hyperfocus set in. Eventually, my creativity and eye for design started making sense of the chaos—finding patterns, making connections, and filling space.

Mutation

Alcohol and Pigment Ink on Paper

I started this sketch of a stylized petri dish a few months ago in my sketchbook, and yesterday, I was inspired to add some color. My first thoughts were to go with a muted color scheme in yellows, browns, and greens, but as I started adding ink to the page, the idea evolved into something different and much more colorful.

Hues shift around the image to show subtle mutations occurring as the microorganisms divide and the colonies grow.