Query di Custom Post per Categoria Tassonomica
Spero che tu stia bene oggi,
La mia domanda è breve: sto cercando di eseguire una query da un custom post type filtrando per una specifica tassonomia;
Per mantenerlo semplice, usiamo il seguente esempio:
Custom Post Type: 'Products';
Tassonomia: 'Categories';
Categorie all'interno di 'Categories':
(1) Category 1 (2) Category 2 (3) Category 3
Vorrei quindi eseguire una query dei post dal Custom Post Type 'Products' che appartengono a 'Category 1'
Come si può fare?
Ecco il mio codice per la query del custom post type e il tentativo di filtrare per la categoria tassonomica.
<?php
// Ottiene la pagina corrente per la paginazione
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = -1; // -1 mostra tutti i post
$do_not_show_stickies = 1; // 0 per mostrare i post in evidenza
$args = array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => 'category-1'
)
),
'paged' => $paged,
'posts_per_page' => $post_per_page,
'order' => 'ASC',
);
$temp = $wp_query; // assegna la query originale a una variabile temporanea per uso successivo
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

Ci sono 3 modi per farlo:
a)
...
'category_name' => 'category-1'
...
b)
...
'taxonomy' => 'category',
'term' => 'category-1',
...
c)
...
'tax_query' => array(
array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => array( 'category-1' ) )
)
...
Maggiori informazioni: http://codex.wordpress.org/Function_Reference/WP_Query

Array tax_query con tassonomia, campo e termini. Dove terms->business è la categoria della tassonomia->job_category
$args = array(
'post_type' => 'featured_job',
'post_status' => 'publish',
'posts_per_page' => 9999999,
'orderby' => 'date',
'order' => 'DES',
'tax_query' => array(
array(
'taxonomy' => 'job_category',
'field' => 'slug',
'terms' => 'business',
),
),
);
