Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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