Created
October 30, 2011 08:11
-
-
Save Viper007Bond/1325696 to your computer and use it in GitHub Desktop.
Limit results to post meta via URL
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 | |
add_action( 'pre_get_posts', 'oomskaap_price_range_limiter' ); | |
function oomskaap_price_range_limiter( $query ) { | |
// Only do something if both the start and end values are set in the URL | |
if ( empty( $_GET['pricestart'] ) || empty( $_GET['priceend'] ) ) | |
return; | |
// Only modify the main query | |
// 3.3 adds "is_main_query" | |
if ( method_exists( $query, 'is_main_query' ) ) { | |
if ( ! $query->is_main_query() ) { | |
return; | |
} | |
} | |
// Pre-3.3 | |
else { | |
global $wp_the_query; | |
if ( $query !== $wp_the_query ) { | |
return; | |
} | |
} | |
// Validate/sanitize | |
$start = absint( $_GET['pricestart'] ); | |
$end = absint( $_GET['priceend'] ); | |
// Add in some more query arguments | |
$query->query_vars['meta_query'][] = array( | |
'key' => 'price', | |
'value' => array( $start, $end ), | |
'compare' => 'BETWEEN', | |
'type' => 'NUMERIC', | |
); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment