Obține toate articolele dintr-un tip de postare personalizat
Încerc să obțin toate articolele dintr-un anumit tip de postare personalizat folosind următorul cod:
$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>';
Deși apelul print_r() arată datele, bucla foreach pare să le ignore și nu afișează nimic. Aveți vreo idee?
Orice ajutor ar fi apreciat
Rezultatul 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] => Titlul Meu
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => titlul-meu
[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
)
)

Poți folosi wp_query()
pentru a face asta
$args = array(
'post_type' => 'auction', // Tipul postării
'posts_per_page' => -1 // Numărul de postări pe pagină (-1 pentru toate)
);
$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;
Documentație pentru WP_Query
https://codex.wordpress.org/Class_Reference/WP_Query

Probabil pentru că get_posts
returnează un obiect, trebuie să inițializați datele postului conform Codex get_posts. Înlocuiți linia 4 cu:
foreach($auctions as $auction) : setup_postdata($auction) {

get_posts() returnează un array http://codex.wordpress.org/Template_Tags/get_posts#Return_Value, utilizarea setup_postdata ar fi necesară pentru a permite folosirea tag-urilor de șablon, precum the_ID()

Rețineți că astfel de bucle folosesc de obicei as $post
pentru a seta variabila globală $post
sau acest lucru trebuie făcut în plus. setup_postdata()
nu face acest lucru.

Încearcă fără get_posts(). În prezent am o funcție similară care funcționează astfel:
$args = array( 'post_type' => 'customPostName', 'post_status' => 'publish');
$pages = get_pages($args);
foreach ( $pages as $page ) {
// Fă ceva
}
Editare: De fapt, nu sunt sigur de ce nu funcționează, deoarece codex clar specifică să folosești echo $post->ID;
cu get_posts. http://codex.wordpress.org/Function_Reference/get_posts#Access_all_post_data
Face vreo diferență pentru tine?
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 a avut dreptate, dar a avut câteva greșeli de scriere... Lipsea un ;
de închidere de la array și la final reset_postdata();
trebuia să devină wp_reset_postdata();
.
Acum ar trebui să funcționeze perfect... Pentru mine funcționează excelent, fără nicio problemă!
Sper că acest lucru vă va fi de ajutor!
