使用WP_Query获取自定义帖子类型的帖子


9

我正在尝试使自定义帖子返回查询并显示,但它返回并显示默认/标准帖子。我如何从我的CPT获得帖子?

$query = new WP_Query( array( 'job_posting_type' => 'Job Post' ) );                  

if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>   
        <div>
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>
        </div>
    <?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
    <!-- show 404 error here -->
<?php endif; ?>

Answers:


15

假设您的自定义帖子类型名为“ job_posting”,则只需将查询更改为:

$query = new WP_Query( array( 'post_type' => 'job_posting' ) );

可以在官方文档中找到。

您可能还要使用很多参数-您可以在我链接到的文档中找到完整列表。我建议您考虑一下posts_per_page(这样您就不会立即返回所有内容),并设置post_status为“发布”,以防万一有任何草稿/私人帖子被退回(无论如何都不应该,但我希望成为安全;))。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.