Ottenere tutti i post da un custom post type
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
)
)

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

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) {

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()

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

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;

'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!
