我在试着WP_Query
要显示数组中的所有帖子,但仅显示状态为“已发布”的帖子,请执行以下操作:
global $wp_query;
$ids = array(130, 132);
$args = array(
\'post_status\' => array(\'publish\', \'pending\', \'draft\', \'auto-draft\', \'future\', \'private\', \'inherit\'),
// \'post_status\' => \'any\', // same output
\'post__in\' => $ids,
\'post_type\' => \'alpha\'
);
$q = new WP_Query($args);
foreach ($q->posts as $post) {
echo $post->id;
}
但是,无论其状态如何,这都会显示帖子id:
// post status
foreach ($ids as $id) {
echo get_post_status($id);
}
这是Bones主题的全新安装,没有插件。如何显示阵列中的所有帖子,而不考虑状态?我一定是在抄本上遗漏了什么。。。
SO网友:Jignesh Patel
请这样尝试:
<?php
global $wp_query;
$ids = array(130,132);
$args = array(
\'post_status\' => array(
\'publish\',
\'pending\',
\'draft\',
\'auto-draft\',
\'future\',
\'private\',
\'inherit\',
),
\'post__in\' => $ids,
\'post_type\' => \'alpha\'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ){
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_ID();
endwhile;
}
wp_reset_postdata();
?>