fix: add count to not used metadata #483

This commit is contained in:
vnmedeiros 2021-03-04 22:59:40 -03:00
parent e75d492421
commit 883ca1cb37
1 changed files with 31 additions and 36 deletions

View File

@ -272,44 +272,39 @@ class REST_Reports_Controller extends REST_Controller {
global $wpdb; global $wpdb;
$string_meta_ids = "'".implode("','", $meta_ids)."'"; $string_meta_ids = "'".implode("','", $meta_ids)."'";
$sql_statement = $wpdb->prepare( $sql_statement = $wpdb->prepare(
"SELECT p.post_title AS 'name', m.meta_key AS id, ((m.total/$total_items) * 100) as fill_percentage "SELECT p.post_title AS 'name', p.id AS id, IFNULL(((m.total/$total_items) * 100), 0) as fill_percentage
FROM
(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 (
SELECT id
FROM $wpdb->posts
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
UNION
SELECT p.post_title AS 'name', m.meta_key AS id, ((m.total/$total_items) * 100) as fill_percentage
FROM FROM
$wpdb->posts p LEFT JOIN
( (
SELECT SELECT meta_key, count(DISTINCT post_id) AS total
mt.meta_key, count(DISTINCT tr.object_id) AS total FROM $wpdb->postmeta
FROM WHERE $wpdb->postmeta.meta_key IN ($string_meta_ids)
$wpdb->term_taxonomy tt AND $wpdb->postmeta.post_id IN (
INNER JOIN $wpdb->term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id SELECT id
INNER JOIN ( FROM $wpdb->posts
SELECT WHERE $wpdb->posts.post_type = '$collection_post_type'
post_id as 'meta_key', concat('tnc_tax_', meta_value) as 'tax_id' )
FROM GROUP BY $wpdb->postmeta.meta_key
$wpdb->postmeta UNION
WHERE SELECT mt.meta_key, count(DISTINCT tr.object_id) AS total
meta_key='_option_taxonomy_id' FROM $wpdb->term_taxonomy tt
) mt ON tt.taxonomy = mt.tax_id INNER JOIN $wpdb->term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE INNER JOIN (
mt.meta_key IN ($string_meta_ids) SELECT post_id as 'meta_key', concat('tnc_tax_', meta_value) as 'tax_id'
AND tr.object_id IN ( FROM $wpdb->postmeta
SELECT id WHERE meta_key='_option_taxonomy_id'
FROM $wpdb->posts ) mt ON tt.taxonomy = mt.tax_id
WHERE $wpdb->posts.post_type = '$collection_post_type' WHERE mt.meta_key IN ($string_meta_ids)
) AND tr.object_id IN (
GROUP BY mt.meta_key) m SELECT id
INNER JOIN $wpdb->posts p on m.meta_key = p.id;" FROM $wpdb->posts
WHERE $wpdb->posts.post_type = '$collection_post_type'
)
GROUP BY mt.meta_key
) m
ON (p.id = m.meta_key)
WHERE p.id IN($string_meta_ids)
"
); );
$res = $wpdb->get_results($sql_statement); $res = $wpdb->get_results($sql_statement);
//return ['t' => $res, 's' => $sql_statement]; //return ['t' => $res, 's' => $sql_statement];