最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding Multiple Post Types to Query

programmeradmin1浏览0评论
This question already has answers here: Query multiple custom post types in single loop (2 answers) Closed 5 years ago.

I'm trying to add a 'Lessons' post type to populate the wp-query as well as the pre-existing post type 'Modules. Preferably I would like them to be combined into one query showing the most recent 6 posts. I've only been able to get them showing in 2 different queries thus far.

<?php 
//Template Name: Last 6 Modules
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'ca_show_last_six_modules' );
function ca_show_last_six_modules(){ 
$modules = new WP_Query(array(
    'post_type'         => 'module',
   'orderby'            => 'menu_order', 
    'order'                 => 'ASC',
    'status'            => 'publish',
    'posts_per_page'    => 6

));
if ( $modules->have_posts() ) : ?>
    <div class="row">   
        <?php while ( $modules->have_posts() ) : $modules->the_post(); ?>
            <?php get_template_part( 'template-parts/modules', 'loop' ); ?>
        <?php endwhile; ?>
    </div>
<?php endif;
}
 genesis(); ?>
This question already has answers here: Query multiple custom post types in single loop (2 answers) Closed 5 years ago.

I'm trying to add a 'Lessons' post type to populate the wp-query as well as the pre-existing post type 'Modules. Preferably I would like them to be combined into one query showing the most recent 6 posts. I've only been able to get them showing in 2 different queries thus far.

<?php 
//Template Name: Last 6 Modules
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'ca_show_last_six_modules' );
function ca_show_last_six_modules(){ 
$modules = new WP_Query(array(
    'post_type'         => 'module',
   'orderby'            => 'menu_order', 
    'order'                 => 'ASC',
    'status'            => 'publish',
    'posts_per_page'    => 6

));
if ( $modules->have_posts() ) : ?>
    <div class="row">   
        <?php while ( $modules->have_posts() ) : $modules->the_post(); ?>
            <?php get_template_part( 'template-parts/modules', 'loop' ); ?>
        <?php endwhile; ?>
    </div>
<?php endif;
}
 genesis(); ?>
Share Improve this question edited May 16, 2019 at 19:16 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 16, 2019 at 18:59 TylerTyler 11 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 1

If you look at the wp_query documentation you will see that it is possible to pass multiple arguments as post_type. Like this:

'post_type' => array( 'module', 'lessons' )

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论