Elencare tutti i post di un custom post type da una determinata categoria?
Ho un custom post type myposttype
e la sua tassonomia si chiama myposttype_categories
.
myposttype_categories
ha diversi termini al suo interno, come foo
e bar
.
La domanda complessa è - come posso elencare tutti i post di myposttype che appartengono a foo
(o bar
)?
Pensavo che questo dovesse funzionare, ma non è così:
$args = array(
'post_type' => 'myposttype',
'myposttype_categories'=> 'foo');
$loop = new WP_Query( $args );
E il loop è sempre vuoto.
Ho provato praticamente ogni parametro di Tassonomia per WP_Query() e ho controllato anche i parametri delle Categorie.
Ci sono post molto vecchi che affrontano questo problema, ma sembra che dopo 3 anni ancora non funzioni...? O mi sta sfuggendo qualcosa?
http://wordpress.org/support/topic/wp_query-and-custom-taxonomies
http://core.trac.wordpress.org/ticket/13582
[modifica]
Ecco come registro il mio post type:
add_action('init', 'myposttype_register');
function myposttype_register() {
$labels = array(
'name' => _x('Myposttype', 'nome generale del tipo di post'),
'singular_name' => _x('Myposttype item', 'elemento myposttype'),
'add_new' => _x('Add Myposttype', 'elemento myposttype'),
'add_new_item' => __('Aggiungi Nuovo Elemento'),
'edit_item' => __('Modifica Elemento'),
'new_item' => __('Nuovo Elemento'),
'view_item' => __('Visualizza Elemento'),
'search_items' => __('Cerca Elementi'),
'not_found' => __('Nessun risultato trovato'),
'not_found_in_trash' => __('Nessun risultato trovato nel Cestino'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail','page-attributes','comments','trackbacks'),
'show_in_nav_menus' => true,
);
register_post_type( 'myposttype' , $args );
}
E la tassonomia:
register_taxonomy("myposttype_categories", array("myposttype"), array("hierarchical" => true, "label" => "Categorie", "singular_label" => "Tipo", "rewrite" => true));

Cosa ne dici di fare una tax_query
?
$args = array(
'post_type' => 'myposttype',
'tax_query'=> array(
'taxonomy' => 'myposttype_categories',
'terms' => array('foo'),
'field' => 'slug',
)
);
$loop = new WP_Query( $args );
var_dump($loop);
Domanda da facepalm, sei sicuro che queste taxonomy/post type esistano e che ci siano post archiviati sotto di esse?
Aggiornamento
La query sembra funzionare bene per me, e riesco a mostrare una lista dei post che ho aggiunto con quel termine/categoria. Ho spostato la tua chiamata register_taxonomy
nella funzione che viene eseguita su init
. Secondo il codex è sconsigliato chiamare register_taxonomy
al di fuori di un'azione e potrebbe essere la causa dei tuoi problemi.
add_action('init', 'myposttype_register');
function myposttype_register() {
$labels = array(
'name' => _x('Myposttype', 'post type general name'),
'singular_name' => _x('Myposttype item', 'myposttype item'),
'add_new' => _x('Add Myposttype', 'myposttype item'),
'add_new_item' => __('Add New Item'),
'edit_item' => __('Edit Item'),
'new_item' => __('New Item'),
'view_item' => __('View Item'),
'search_items' => __('Search Items'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail','page-attributes','comments','trackbacks'),
'show_in_nav_menus' => true,
);
register_post_type( 'myposttype' , $args );
register_taxonomy("myposttype_categories", array("myposttype"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Type", "rewrite" => true));
}
E la query:
$args = array(
'post_type' => 'myposttype',
'myposttype_categories'=> 'foo');
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p><?php
endwhile;
}
wp_reset_query();
Per quel che vale, anche i seguenti funzionano come argomenti, anche se visto che stai interrogando una sola taxonomy probabilmente non hai bisogno di usare tax_query
. L'ho usato inizialmente perché pensavo avessi bisogno di trovare post in entrambi i termini.
$args = array( 'myposttype_categories'=> 'foo' );
e
$args = array(
'post_type' => 'myposttype',
'tax_query' => array(
array(
'taxonomy' => 'myposttype_categories',
'terms' => array('foo'),
'field' => 'slug'
)
)
);

Mi restituisce un oggetto vuoto, sei sicuro che funzioni? A proposito, volevo visualizzare solo foo
o bar
, non entrambi :) E sì, la mia tassonomia esiste, ci sono post archiviati sotto di essa.

Sì, ho appena verificato, sembra che tax_query funzioni correttamente solo con il tipo di post post
, mentre per i post type personalizzati restituisce nulla per array come: 'terms' => array('foo'
) o tutto per stringhe: 'terms' => 'foo'
. Beh, sembra che ancora non possiamo combinare post type personalizzati E filtraggio per termini? :(

Hai detto foo (o bar) quindi ho interpretato male pensando che avessi elementi in entrambe le categorie. Questa query cerca elementi che sono in entrambe, quindi se non ne hai nessuno otterresti un oggetto vuoto. Per quel che vale, tax_query
funziona con qualsiasi tipo di post. Probabilmente dovresti postare il tuo codice per la registrazione del post type e delle tassonomie. Adatterò il mio codice, anche se se stai facendo una query semplice, quello che hai postato dovrebbe funzionare, ed è per questo che penso tu debba postare più codice.

PS - Non volevo "urlare" e mettere tutto in grassetto, ho dimenticato che # è il markdown per il grassetto. Mannaggia.

Va bene, ho appena aggiornato la mia domanda con i codici di registrazione del custom post type e della tassonomia.

Cavolo, hai ragione, funziona, ma sembra non nel mio caso. Hai idea di cosa potrebbe romperlo? La tassonomia esiste, i termini esistono, il custom post type esiste, ma non ottengo comunque output. E quando uso lo stesso codice su Twenty Twelve funziona a meraviglia. Ho persino provato a usare questo codice subito dopo il tag body prima di eventuali loop ecc. ma nulla sembra aiutare :/
