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)

Creating Your First AppJS App with Custom Chrome

AppJS is no longer being updated or maintained. For a similar platform, check out node-webkit which is backed by Intel Open Source and is consistently being improved and updated. I’ll write a new tutorial for node-webkit sometime in the future. Also, despite the legacy name, node-webkit uses Chromium as its base, so it is now using the Blink rendering engine.

Over the past few months, I’ve been studying up and trying out several cross-platform development solutions and one of the more interesting ones I’ve found, as a web designer and developer, is AppJS. It allows development for the desktop for all three major desktop operating systems using the mix of technologies commonly known as HTML5 and node.js.

Of course, I’ve also been using Adobe AIR since it was introduced, though AIR for HTML isn’t all that great in my experience. AppJS, on the other hand, uses the Chromium Embedded Framework which renders content beautifully, is super fast and has all of the latest and greatest features HTML5 has to offer without awkward workarounds.

Before getting started, it is worth mentioning that AppJS is still a relatively new project and documentation is sparse at the moment, but it is being worked on. I’m also not attempting to compile and package a completed app just yet, so I will be using the distributable version and replacing the content files throughout this tutorial.