Skip to content

Instantly share code, notes, and snippets.

@Viper007Bond
Created October 30, 2011 08:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Viper007Bond/1325696 to your computer and use it in GitHub Desktop.
Save Viper007Bond/1325696 to your computer and use it in GitHub Desktop.
Limit results to post meta via URL
<?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