diff --git a/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php b/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php index d60c193e7..b12bb1e55 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php @@ -274,7 +274,7 @@ class REST_Reports_Controller extends REST_Controller { $sql_statement = $wpdb->prepare( "SELECT p.post_title AS 'name', m.meta_key AS id, ((m.total/$total_items) * 100) as fill_percentage FROM - (SELECT meta_key, count(*) AS total + (SELECT meta_key, count(DISTINCT post_id) AS total FROM $wpdb->postmeta WHERE $wpdb->postmeta.meta_key IN ($string_meta_ids) AND $wpdb->postmeta.post_id IN ( @@ -283,10 +283,36 @@ class REST_Reports_Controller extends REST_Controller { WHERE $wpdb->posts.post_type = '$collection_post_type' ) GROUP BY $wpdb->postmeta.meta_key) m - INNER JOIN $wpdb->posts p on m.meta_key = p.id;" + INNER JOIN $wpdb->posts p on m.meta_key = p.id + UNION + SELECT p.post_title AS 'name', m.meta_key AS id, ((m.total/$total_items) * 100) as fill_percentage + FROM + ( + SELECT + mt.meta_key, count(DISTINCT tr.object_id) AS total + FROM + $wpdb->term_taxonomy tt + INNER JOIN $wpdb->term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id + INNER JOIN ( + SELECT + post_id as 'meta_key', concat('tnc_tax_', meta_value) as 'tax_id' + FROM + $wpdb->postmeta + WHERE + meta_key='_option_taxonomy_id' + ) mt ON tt.taxonomy = mt.tax_id + WHERE + mt.meta_key IN ($string_meta_ids) + AND tr.object_id IN ( + SELECT id + FROM $wpdb->posts + WHERE $wpdb->posts.post_type = '$collection_post_type' + ) + GROUP BY mt.meta_key) m + INNER JOIN $wpdb->posts p on m.meta_key = p.id;" ); $res = $wpdb->get_results($sql_statement); - //return $sql_statement; + //return ['t' => $res, 's' => $sql_statement]; return $res; }