How to join result of different taxonomies?

I need to search properties by cities and areas at the same time. So I created two query to take all the references of the cities, and the areas:

$city_query = “SELECT *
FROM $wpdb->terms t
INNER JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id
WHERE tt.taxonomy = ‘property-city’ AND LOWER(t.name) LIKE ‘%s’ “;
$like = ‘%colorado%’;
$result = $wpdb->get_results($wpdb->prepare($city_query, $like), OBJECT);
$city_ids = array();

foreach($result as $city)
{
array_push($city_ids, $city->term_id);
}

$area_query = “SELECT *
FROM $wpdb->terms t
INNER JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id
WHERE tt.taxonomy = ‘property-area’ AND LOWER(t.name) LIKE ‘%s’ “;
$result = $wpdb->get_results($wpdb->prepare($area_query, $like), OBJECT);
$area_ids = array();

foreach($result as $area)
{
array_push($area_ids, $area->term_id);
}

until here there is no problem ’cause I taken only the reference of the taxonomies. The problem is here:

$args = array(
‘post_type’ => ‘zoacres-property’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => 12,
‘order’ => ‘DESC’,
‘paged’ => 1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘property-city’,
‘field’ => ‘term_id’,
‘terms’ => $city_ids,
‘operator’ => ‘IN’,
),
array(
‘taxonomy’ => ‘property-area’,
‘field’ => ‘term_id’,
‘terms’ => $area_ids,
‘operator’ => ‘IN’,
)
)
);

$query = new WP_Query($args);
$found_posts = $query->found_posts;

Essentially, this query isn’t returning anything ’cause there are no properties for colorado which have as taxonomy area, but there are only properties in city taxonomy.

Is it possible to return the results that there are at least in a taxonomy, but searching for all of them?

Read more here:: How to join result of different taxonomies?

Leave a Reply

Your email address will not be published. Required fields are marked *