Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cameronjonesweb
Created March 5, 2018 00:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronjonesweb/722fb8cfa518902fc1f92a868bd0b6ae to your computer and use it in GitHub Desktop.
Save cameronjonesweb/722fb8cfa518902fc1f92a868bd0b6ae to your computer and use it in GitHub Desktop.
<?php
/**
* A custom WP_Query object that will always be empty
*/
$query = new WP_Query( array(
'post__in' => array( 0 )
) );
<?php
/**
* Prevent searches with no search terms from returning any posts
*
* @param $query object The query object to manipulate
*/
function cameronjonesweb_empty_search_term_results( $query ) {
if( !is_admin() && $query->is_main_query() && $query->is_search() && empty( get_search_query() ) ) {
$query->set( 'post__in', array( 0 ) );
}
}
add_action( 'pre_get_posts', 'cameronjonesweb_empty_search_term_results' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment