Created
April 15, 2012 20:44
-
-
Save luisabarca/2394739 to your computer and use it in GitHub Desktop.
WordPress: Search with multiple taxonomies
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 | |
add_action('pre_get_posts', '_custom_search'); | |
function _custom_search() | |
{ | |
global $wp_query; | |
if ( $wp_query->query_vars['post_type'] != REALTY_POST_TYPE || | |
!$wp_query->is_post_type_archive ) { | |
return false; | |
} | |
$tax_query = array(); | |
$operacion = get_query_var('operacion'); | |
$ubicacion = get_query_var('ubicacion'); | |
$area = get_query_var('area'); | |
$precio = get_query_var('precio'); | |
// Tipo de operaciones | |
if (!empty($operacion) && $operacion > 0) { | |
$tax_query[] = array( | |
'taxonomy' => 'jab_realty_operation', | |
'field' => 'id', | |
'terms' => $operacion | |
); | |
} | |
// location | |
if (!empty($ubicacion) && $ubicacion > 0) { | |
$tax_query[] = array( | |
'taxonomy' => 'jab_realty_location', | |
'field' => 'id', | |
'terms' => $ubicacion | |
); | |
} | |
// Precios | |
if (!empty($precio) && $precio > 0) { | |
$tax_query[] = array( | |
'taxonomy' => 'jab_realty_pricerange', | |
'field' => 'id', | |
'terms' => $precio | |
); | |
} | |
if ( sizeof($tax_query) > 0 ) { | |
$tax_query['relation'] = 'AND'; | |
$wp_query->query_vars['tax_query'] = $tax_query; | |
} | |
// Area | |
if (!empty($area) && $area > 0) { | |
$meta_query = array(); | |
$meta_query[] = array( | |
'key' => 'jab_realty_area', | |
'value' => $area, | |
'compare' => '=', | |
'type' => 'NUMERIC' | |
); | |
$wp_query->query_vars['meta_query'] = $meta_query; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment