Sorry for reviving this post, but it was on the front page and I noticed that it’s very old too late… Here’s my take on this problem:
// This will filter the shortcode attributes and will insert custom
// value for the "cat" parameter
function filter_gmaps_shortcode_atts( $atts ) {
// We add a custom value in the $cat parameter
if ( is_tax( 'cities' ) ) {
$atts['cat'] = 'filter_taxonomy_cities';
} elseif ( is_tax( 'regions' ) ) {
$atts['cat'] = 'filter_taxonomy_regions';
}
return $atts;
}
add_filter( 'gmshc_shortcode_atts', 'filter_gmaps_shortcode_atts', 10 );
// This filters the WordPress query and checks for our custom values from above
// We then modify the query to look for the proper post type and taxonomy
function filter_gmaps_get_post( &$wp_query ) {
if ( isset( $wp_query->query_vars['category__in'] ) ) {
$queried_obj = get_queried_object();
if ( in_array( 'filter_taxonomy_cities', $wp_query->query_vars['category__in'] ) || in_array( 'filter_taxonomy_regions', $wp_query->query_vars['category__in'] ) ) {
unset( $wp_query->query_vars['category__in'] );
$wp_query->query_vars['tax_query'] = array(
array(
'taxonomy' => $queried_obj->taxonomy,
'terms' => array( intval( $queried_obj->term_id ) ),
'field' => 'id'
)
);
$wp_query->query_vars['post_type'] = 'hotels';
}
}
}
add_action( 'pre_get_posts', 'filter_gmaps_get_post', 10 );
Basically we filter the shortcode attributes when we’re on a “cities” or “regions” taxonomy page and add a custom value for the “cat” parameter.
In the pre_get_posts
action fired by WP_Query::get_posts()
we check if the custom values are present in the category__in
parameter – if so, we unset the category__in
parameter and add a tax_query
parameter for the current taxonomy.