Ottenere un termine tramite meta personalizzato e tassonomia
Come ottenere un termine tramite meta personalizzato del termine e tassonomia o come filtrare tax_query
tramite meta del termine invece di slug
/id
?
function custom_pre_get_posts($query)
{
global $wp_query;
if ( !is_admin() && is_shop() && $query->is_main_query() && is_post_type_archive( "product" ))
{
$term = ???get_term_by_meta_and_taxonomy???('custom_meta_term','my_taxonomy');
$t_id = $term['term_id'];
$tax_query = array
(
array
(
'taxonomy' => 'my_taxoomy',
'field' => 'id',
'terms' => $t_id
)
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'custom_pre_get_posts' );

Questa dovrebbe essere la risposta accettata poiché rappresenta la migliore pratica per questo caso.

Concordo, ho verificato e questo metodo permette di recuperare un termine tramite una chiave e un valore di meta termine. Assicurati di impostare hide_empty su false se il tuo termine non ha ancora post associati.

Basandosi sulla risposta di ilgıt-yıldırım sopra, sia l'istruzione get_term_meta
che le istruzioni $key == 'meta_value'
devono contenere $term>term_id
.
Ecco un esempio completo che include la richiesta personalizzata $wp_query
:
$term_args = array( 'taxonomy' => 'your-taxonomy' );
$terms = get_terms( $term_args );
$term_ids = array();
foreach( $terms as $term ) {
$key = get_term_meta( $term->term_id, 'term-meta-key', true );
if( $key == 'term-meta-value' ) {
// inserisce l'ID nell'array
$term_ids[] = $term->term_id;
}
}
// Argomenti del Loop
$args = array(
'post_type' => 'posts',
'tax_query' => array(
array(
'taxonomy' => 'your-taxonomy',
'terms' => $term_ids,
),
),
);
// La Query
$featured = new WP_Query( $args );

Dovrai ciclare attraverso ciascuno dei termini nella tua condizione di query principale. Supponendo che ci saranno probabilmente più termini con i dati personalizzati, dovrai poi passare un array di ID alla tua tax query.
Ad esempio, ciclando attraverso ogni termine per verificare la presenza di meta personalizzati:
$term_args = array(
'taxonomy' => $taxonomy_name,
);
$terms = get_terms( $term_args );
$term_ids = array();
foreach( $terms as $term ) {
$key = get_term_meta( $term->ID, 'meta_key', true );
if( $key == 'meta_value' ) {
// aggiungi l'ID all'array
$term_ids[] = $term->ID;
}
}
Alla fine otterrai la variabile $term_ids che contiene un array degli ID dei termini che stai cercando. Puoi passarla alla tua tax query.

get_term_meta( $term->ID, 'meta_key', true );
dovrebbe essere get_term_meta( $term->term_id, 'meta_key', true );
poiché $term
sarebbe un oggetto WP_Term
.
