obtener todas las entradas de un tipo de publicación personalizada
Estoy tratando de obtener todas las entradas de un tipo de publicación personalizada específico usando el siguiente código:
$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>';
Aunque la llamada print_r() muestra datos, el foreach parece ignorarlos y no imprime nada. ¿Alguna idea?
Cualquier ayuda sería apreciada
Salida de 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] => Mi Título
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => mi-titulo
[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
)
)

Puedes usar wp_query()
para que esto funcione
$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;
Documentación para WP_Query
https://codex.wordpress.org/Class_Reference/WP_Query

Quizás porque get_posts
devuelve un objeto, necesitas configurar los datos del post según Códice get_posts. Reemplazando la línea 4:
foreach($auctions as $auction) : setup_postdata($auction) {

get_posts() devuelve un array http://codex.wordpress.org/Template_Tags/get_posts#Return_Value, ejecutar setup_postdata se usaría para permitir el uso de etiquetas de plantilla, como the_ID()

Nota que típicamente estos loops son as $post
para establecer el global $post
o necesita hacerse adicionalmente. setup_postdata()
no hace esto.

Prueba sin usar get_posts(). Actualmente tengo una función similar que funciona así:
$args = array( 'post_type' => 'customPostName', 'post_status' => 'publish');
$pages = get_pages($args);
foreach ( $pages as $page ) {
// Hacer algo
}
Edición: En realidad no estoy seguro de por qué esto no funciona ya que el codex claramente dice usar echo $post->ID;
con get_posts. http://codex.wordpress.org/Function_Reference/get_posts#Access_all_post_data
¿Esto hace alguna diferencia para ti?
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 tenía razón, pero tenía algunos errores tipográficos... Faltaba un ;
de cierre en el array y al final reset_postdata();
debe ser wp_reset_postdata();
.
Esto debería funcionar bien ahora... ¡A mí me funciona perfectamente, sin ningún problema!
¡Espero que esto ayude!
