Stessa tassonomia per diversi tipi di post: come nascondere i vuoti in un tipo specifico?
Ho una tassonomia "artist" per due diversi tipi di post personalizzati ("project" e "product").
Devo visualizzare un elenco di "artisti", ma solo se hanno "product" correlati e solo se quei prodotti sono ancora disponibili in magazzino.
$artists = get_terms( array(
'taxonomy' => 'artist',
'hide_empty' => 1,
) );
Questo include artisti che hanno "project" ma nessun "product". È possibile specificare a quale tipo di post dovrebbe riferirsi l'argomento "hide_empty"?
MODIFICA Ecco quello che ho fatto finora.
/*
Utile quando una tassonomia si applica a più tipi di post
fonte: http://wordpress.stackexchange.com/a/24074/82
*/
function get_terms_by_post_type( $taxonomies, $post_types ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT t.*, COUNT(*) from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
GROUP BY t.term_id",
join( "', '", $post_types ),
join( "', '", $taxonomies )
);
$results = $wpdb->get_results( $query );
return $results;
}
$artists = get_terms_by_post_type( array('artist'), array('product'));
//*
if( !empty($artists) && !is_wp_error( $artists ) ){
// filtra gli artisti con prodotti esauriti
foreach($artists as $k=>$artist){
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'artist',
'field' => 'id',
'terms' => array( $artist->term_id )
),
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$query = new WP_Query($args);
echo $artist->slug;
pr($query->post_count);
if($query->post_count<1){
unset($artists[$k]);
}
}
}
Funziona quasi, ma non rimuove i prodotti esauriti.

Un'altra soluzione allo stesso problema che è leggermente più breve e meno complessa:
function get_terms_by_posttype($taxonomy, $postType) {
// Ottieni tutti i termini che hanno articoli
$terms = get_terms($taxonomy, [
'hide_empty' => true,
]);
// Rimuovi i termini che non hanno alcun articolo nel tipo di post corrente
$terms = array_filter($terms, function ($term) use ($postType, $taxonomy) {
$posts = get_posts([
'fields' => 'ids',
'numberposts' => 1,
'post_type' => $postType,
'tax_query' => [[
'taxonomy' => $taxonomy,
'terms' => $term,
]],
]);
return (count($posts) > 0);
});
// Restituisci ciò che ci rimane
return $terms;
}

Grazie a questa risposta, sono riuscito a vedere la luce. Ecco come interrogare i termini di tassonomia relativi a prodotti (woocommerce) che sono "in magazzino". Utilizza sia tax_query (per relazionare i prodotti alla tassonomia) che meta_query (per filtrare i prodotti esauriti).
$artists = get_terms_by_post_type( array('artist'), array('product'));
if( !empty($artists) && !is_wp_error( $artists ) ){
// filtra gli artisti di prodotti esauriti
foreach($artists as $k=>$artist){
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' =>array(
'relation' => 'AND',
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '='
)
,
array(
'key' => '_stock',
'value' => '0',
'compare' => '>'
)
,
array(
'key' => '_stock',
'value' => '',
'compare' => '!='
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'artist',
'field' => 'id',
'terms' => array( $artist->term_id )
),
)
);
$query = new WP_Query($args);
if($query->post_count<1){
unset($artists[$k]);
}
}
}
function get_terms_by_post_type( $taxonomies, $post_types ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT t.*, COUNT(*) from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
GROUP BY t.term_id",
join( "', '", $post_types ),
join( "', '", $taxonomies )
);
$results = $wpdb->get_results( $query );
return $results;
}
