WP_Query per ID categoria e custom post_type
20 nov 2014, 02:17:12
Visualizzazioni: 120K
Voti: 13
Ho bisogno di interrogare tutti i post che appartengono a una determinata categoria (predefinita, non personalizzata) e a un custom post type. Semplicemente questo. Il fatto che non funzioni, per me, è ridicolo. A meno che non mi stia sfuggendo qualcosa?
Ecco cosa ho provato:
$args=array(
'posts_per_page' => 50,
//'taxonomy' => 'category',
'post_type' => 'my_custom_type'
'category__in' => array($cat_id),
);
$wp_query = new WP_Query( $args );
poi
$args=array(
'posts_per_page' => 50,
'post_type' => 'my_custom_type'
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $cat_id,
),
),
);
$wp_query = new WP_Query( $args );
e naturalmente
$args=array(
'posts_per_page' => 50,
'post_type' => 'my_custom_type'
'category' => $cat_id,
);
$wp_query = new WP_Query( $args );
inoltre, alcune combinazioni di aggiunta/ridenominazione/rimozione delle chiavi $args
.
Ottenere tutti i post per tipo di post e poi ciclarli e filtrarli per categoria non è un'opzione efficace, credo.
Per favore aiutatemi.

ᴍᴇʜᴏᴠ
402
Commenti
Mostra i restanti 1 commenti
Tutte le risposte alla domanda
3
1
Prova questo, funziona per me.
$args=array(
'posts_per_page' => 50,
'post_type' => 'my_custom_type'
'cat' => $cat_id,
);
$wp_query = new WP_Query( $args );
Parametri delle Categorie
cat (int): usa l'ID della categoria.
category_name (string): usa lo slug della categoria (NON il nome).
category__and (array): usa l'ID della categoria.
category__in (array): usa l'ID della categoria.
category__not_in (array): usa l'ID della categoria.

kunal Gauswami
159
12 set 2016 13:25:59
Commenti
1
questo ha funzionato per me.
$args=array(
'posts_per_page' => 50,
'post_type' => 'my_custom_type'
'tax_query' => array(
array(
'taxonomy' => 'category', //verifica il nome della tassonomia nel tuo database
'field' => 'id',
'terms' => $cat_id,
),
),
);
$wp_query = new WP_Query( $args );

Ritchie
191
4 nov 2019 14:10:00
0
Ottieni articoli per ID della categoria
$categories = get_categories();
$loop = new WP_Query([
'posts_per_page' => 6, // Numero di articoli da mostrare
'post_type' => 'post', // Tipo di contenuto (articoli)
'tax_query' => [ // Query per tassonomia
[
'taxonomy' => 'category', // Tassonomia categoria
'field' => 'id', // Campo da usare per il confronto (ID)
'terms' => $categories[0]->term_id // ID della prima categoria
],
],
]);

Jxk427 Egamberdiyev
23
2 feb 2024 15:53:53
Domande correlate
3
risposte
15
risposte
3
risposte