Created
March 5, 2018 00:07
-
-
Save cameronjonesweb/722fb8cfa518902fc1f92a868bd0b6ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A custom WP_Query object that will always be empty | |
*/ | |
$query = new WP_Query( array( | |
'post__in' => array( 0 ) | |
) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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