显示来自多个自定义 post 类型的特定类别的 posts

Display posts of certain category from multiple custom post types

我正在尝试在网站上创建一个 "Meal of the month" 部分。菜单分为自定义 post 类型,因此我需要能够从多个 post 类型循环分类。

这是我目前的代码,完全没有做任何事情:

<div class="maaltijd-vdm col-1-1">
                <?php   $mvdm = new WP_Query( array( 'category_name' => 'mvdm', 'posts_per_page' => 1 ) ); ?>
                <?php   while ($mvdm->have_posts()) : $mvdm->the_post(); ?>

                    <div class="mvdm-thumb">
                        <?php the_thumbnail(); ?>
                    </div>

                    <div class="description">
                        <h3><?php the_title(); ?></h3>
                        <p><?php get_the_mvdm(); ?></p>
                    </div>

                <?php   endwhile; wp_reset_postdata(); ?>

            </div>

非常感谢您的帮助!

*get_the_mvdm 是自定义函数

*我已经在同一页面中使用相同的代码(除了变量名)有一个新闻循环

要查询多个 post 类型,您可以将 post 类型 slug 的数组传递给查询。

$args = array(
    'post_type'         => array('cpt1', 'cpt2'),   /* the names of you custom post types */
    'category_name'     => 'mvdm',
    'posts_per_page'    => -1                       /* get all posts */
)

$mvdm = new WP_Query( $args );

您必须使用 tax_query 才能按类别获取帖子。

试试这个代码:

$tags_args = array(
                'post_type' => array(cpt1, cpt2, cpt3 ....),
                'posts_per_page' => 999,
                'order' => 'DESC',
                'tax_query' => array(
                                    array(
                                        'taxonomy' => 'Your Taxonomy',
                                        'field' => 'slug',
                                        'terms' => 'Your term slug'
                                    )
                                )
                            );
            $tags_qry = new WP_Query($tags_args);

            while($tags_qry->have_posts()) :
                $tags_qry->the_post();

                // Your Code
            endwhile

希望您找到解决方案。

这对我有用:

$args = array(
    'post_type' => array(
        'news',
        'agenda'
    ),
    'posts_per_page' => '8',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'news_cat',
            'field' => 'slug',
            'terms' => 'bedrijven'
        ),
        array(
            'taxonomy' => 'agenda_cat',
            'field' => 'slug',
            'terms' => 'bedrijven'
        )
    )
);