Elencare tutti i post di un custom post type da una determinata categoria?

16 feb 2013, 01:40:12
Visualizzazioni: 31.8K
Voti: 2

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));
3
Commenti

come è strutturato il tuo codice che itera sui risultati della tua query? come è strutturato il tuo codice che registra il tuo post type e la tassonomia?

Milo Milo
16 feb 2013 01:57:30

Ho controllato tutto da WP_Query all'odiato query_posts, sia la tassonomia che i custom post type sono ok, anche il codice che itera i risultati funziona. Il filtraggio dei custom post type usando i termini funziona per te? Perché temo che semplicemente non funzioni e che quello sia il problema. Tutti gli esempi nel Codex usano il post type 'post'...

Wordpressor Wordpressor
16 feb 2013 06:09:43

il tuo codice sopra funziona bene per me, ecco perché ho chiesto informazioni sul tuo codice di registrazione del post type e della tassonomia, e su come fosse strutturato il tuo loop. È effettivamente possibile fare quello che vuoi, hai solo un problema con il tuo codice da qualche altra parte.

Milo Milo
16 feb 2013 06:28:43
Tutte le risposte alla domanda 1
7

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'
        )
    )
);
16 feb 2013 03:59:12
Commenti

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.

Wordpressor Wordpressor
16 feb 2013 05:37:47

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? :(

Wordpressor Wordpressor
16 feb 2013 06:04:04

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.

helgatheviking helgatheviking
16 feb 2013 16:23:21

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

helgatheviking helgatheviking
16 feb 2013 16:24:22

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

Wordpressor Wordpressor
16 feb 2013 18:17:47

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 :/

Wordpressor Wordpressor
17 feb 2013 00:43:27

Penso che ci sia un conflitto nel tuo tema. Dovrai analizzare il file functions.php pezzo per pezzo. Potresti provare a commentare tutto e poi riaggiungere ogni funzione gradualmente? È la cosa peggiore.

helgatheviking helgatheviking
17 feb 2013 01:35:23
Mostra i restanti 2 commenti