从自定义帖子类型获取所有帖子

时间:2013-05-28 作者:leemon

我正在尝试使用以下代码从特定自定义帖子类型获取所有帖子:

$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>\';
虽然print\\u r()调用显示数据,但foreach似乎忽略了它,不打印任何内容。有什么想法吗?

任何帮助都将不胜感激

print\\u 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] => My Title
        [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
    )   
) 

4 个回复
SO网友:Gregory

您可以使用wp_query() 为了让它起作用

$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;
的文档WP_Query https://codex.wordpress.org/Class_Reference/WP_Query

SO网友:stellarcowboy

也许是因为get_posts 返回一个对象,您需要根据Codex get_posts. 更换第4行:

foreach($auctions as $auction) : setup_postdata($auction) { 

SO网友:iyrin

在不使用get\\u posts()的情况下进行尝试。我目前有一个类似的功能,其工作原理如下:

$args = array( \'post_type\' => \'customPostName\', \'post_status\' => \'publish\');
$pages = get_pages($args);
foreach ( $pages as $page ) {
    // Do something
    }
编辑:事实上,我不知道为什么这不起作用,因为codex明确表示要使用echo $post->ID; 使用get\\u帖子。http://codex.wordpress.org/Function_Reference/get_posts#Access_all_post_data

这对你有什么影响吗?

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

        echo $option;

SO网友:vagiag

        \'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实际上是对的,但有一些拼写错误。。。结束了; 阵列中和末尾缺少reset_postdata(); 必须成为wp_reset_postdata();.现在应该可以了。。。这对我来说很好,没有任何问题!希望这会有所帮助!

结束

相关推荐