Created
October 19, 2011 07:27
-
-
Save Viper007Bond/1297669 to your computer and use it in GitHub Desktop.
Determining if modifying the main query or not in WordPress
This file contains hidden or 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 | |
# See bigdawggi's comment below for a good combined example | |
// Pre-3.3 | |
function cf_google_search_kill_wp_search( $where, $query ) { | |
global $wp_the_query; | |
if( ! is_admin() && $query->is_search() && $query === $wp_the_query ) { | |
$where = ' AND 1=0'; | |
} | |
return $where; | |
} | |
add_filter( 'posts_where', 'cf_google_search_kill_wp_search', 10, 2 ); | |
// Post-3.3 | |
function cf_google_search_kill_wp_search( $where, $query ) { | |
if ( ! is_admin() && $query->is_search() && $query->is_main_query() ) { | |
$where = ' AND 1=0'; | |
} | |
return $where; | |
} | |
add_filter( 'posts_where', 'cf_google_search_kill_wp_search', 10, 2 ); | |
?> |
Oops, I was missing the is_search()
check in my example. I also meant to add onto $where
rather than replacing it, but now that I think of it replacing is better.
But anyway, yeah, I like your example and that'll work well. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Glad to see an is_main_query() method, how 'bout this?