Ottenere tutti i post da un custom post type

28 mag 2013, 23:28:01
Visualizzazioni: 35.5K
Voti: 12

Sto cercando di ottenere tutti i post da un particolare custom post type usando il seguente codice:

$auctions = get_posts(array('post_type' => 'auction'));
print_r($auctions);
echo '<select>';
foreach ($auctions as $auction) {
    echo '<option value="' . $auction->ID . '">' . $auction->post_title . '</option>';
}
echo '</select>';

Sebbene la chiamata print_r() mostri i dati, il foreach sembra ignorarli e non stampa nulla. Qualche idea?

Qualsiasi aiuto sarebbe apprezzato

Output di print_r():

Array (
    [0] => WP_Post Object (
        [ID] => 36
        [post_author] => 1
        [post_date] =>    2013-05-19 10:58:45
        [post_date_gmt] => 2013-05-19 08:58:45
        [post_content] =>
        [post_title] => Il Mio Titolo
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] =>
        [post_name] => my-title
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2013-05-24 09:55:53
        [post_modified_gmt] => 2013-05-24 07:55:53
        [post_content_filtered] =>
        [post_parent] => 0
        [guid] => http://domain.com/?post_type=auction&p=36
        [menu_order] => 0
        [post_type] => auction
        [post_mime_type] =>
        [comment_count] => 0
        [filter] => raw
    )   
) 
5
Commenti

Cosa restituisce print_r( $auctions );? (Aggiungilo alla tua domanda; sarà molto difficile da leggere nei commenti.)

Pat J Pat J
28 mag 2013 23:38:22

Dati print_r() aggiunti

leemon leemon
28 mag 2013 23:45:17

L'istruzione <select>...</select> compare nell'HTML generato? Se visualizzi il sorgente della tua pagina, appare corretto?

Pat J Pat J
28 mag 2013 23:51:38

Ops... un errore di battitura che ho fatto ha impedito l'output corretto. Grazie comunque!

leemon leemon
29 mag 2013 00:05:59

@user1991185 quel refuso era il problema e la domanda è risolta?

Rarst Rarst
29 mag 2013 00:49:51
Tutte le risposte alla domanda 4
2
14

Puoi utilizzare wp_query() per far funzionare questo codice

$args = array(
        'post_type' => 'auction',
        'posts_per_page' => -1
    );
$query = new WP_Query($args);
if ($query->have_posts()): 
    echo '<select>';
    while ($query->have_posts()): $query->the_post();
            echo '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
    endwhile;
    echo '</select>';
    wp_reset_postdata();
endif;

Documentazione per WP_Query https://codex.wordpress.org/Class_Reference/WP_Query

7 giu 2019 20:22:54
Commenti

per favore aggiorna il codice con wp_reset_postdata(). C'è un errore.

Ashish Yadav Ashish Yadav
2 mar 2020 10:09:05

Ho ricevuto un errore perché manca un punto e virgola alla riga 4 (dopo l'array $args). Sarebbe bene correggerlo.

Arrowcatch Arrowcatch
22 giu 2020 09:07:00
4

Forse perché get_posts restituisce un oggetto, è necessario configurare i dati del post come indicato nella Codex get_posts. Sostituisci la riga 4 con:

foreach($auctions as $auction) : setup_postdata($auction) { 
28 mag 2013 23:58:27
Commenti

get_posts() restituisce un array http://codex.wordpress.org/Template_Tags/get_posts#Return_Value, setup_postdata verrebbe utilizzato per consentire l'uso dei tag template, come the_ID()

Andrew Bartel Andrew Bartel
29 mag 2013 00:02:19

Ah, sì, lo vedo più avanti nella pagina. Grazie @AndrewBartel

stellarcowboy stellarcowboy
29 mag 2013 00:05:14

Nota che tali loop sono tipicamente as $post per impostare la variabile globale $post oppure deve essere fatto separatamente. setup_postdata() non lo fa.

Rarst Rarst
29 mag 2013 00:52:31

Ottimo rilevamento @Rarst.. buono a sapersi.

stellarcowboy stellarcowboy
29 mag 2013 01:04:06
0

Prova senza utilizzare get_posts(). Attualmente ho una funzione simile che funziona così:

$args = array( 'post_type' => 'customPostName', 'post_status' => 'publish');
$pages = get_pages($args);
foreach ( $pages as $page ) {
    // Fai qualcosa
    }

Modifica: In realtà non sono sicuro del perché questo non funzioni dato che il codex indica chiaramente di usare echo $post->ID; con get_posts. http://codex.wordpress.org/Function_Reference/get_posts#Access_all_post_data

Questo fa qualche differenza per te?

    foreach ($auctions as $auction) {
        $option = '<option value="';
        $option .= $auction->ID;
        $option .= '">';
        $option .= $auction->post_title;
        $option .= '</option>';

        echo $option;
28 mag 2013 23:59:26
0
        'post_type' => 'auction',
        'posts_per_page' => -1,
        'post_status' => 'publish',
    );
$query = new WP_Query($args);
if ($query->have_posts() ) : 
    echo '<select>';
    while ( $query->have_posts() ) : $query->the_post();
            echo '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
    endwhile;
    echo '</select>';
    wp_reset_postdata();
endif;

@gregory aveva effettivamente ragione, ma c'erano alcuni errori di battitura... Mancava un ; di chiusura dall'array e alla fine reset_postdata(); deve diventare wp_reset_postdata();. Questo dovrebbe funzionare perfettamente ora... Funziona benissimo per me, senza alcun problema! Spero che questo sarà d'aiuto!

17 dic 2019 19:31:18