显示每个自定义分类的一个帖子

时间:2019-03-07 作者:joloshop

我试图只发布每个自定义分类法的一篇文章APP_TAX_STORE 我的wp_query 如下所示:

// show all alternative active coupons for this category from related store

                $tax = get_the_terms($id, \'coupon_category\');
                $args = array(
                    \'post_type\' => APP_POST_TYPE,
                    \'post_status\' => \'publish\',
                    \'posts_per_page\' => 5,
                    \'meta_key\' => \'clpr_topcoupon\',
                    APP_TAX_TAG => \'gutschein\',
                    \'orderby\'  => array(
                        \'meta_value_num\' => \'DESC\',
                        \'post_date\'      => \'DESC\',
                        ),                      
                    \'tax_query\' => array( 
                    \'relation\' => \'AND\',
                        array(
                        \'taxonomy\' => \'coupon_category\',
                        \'field\'    => \'slug\',
                        \'terms\'    => $tax[0]->slug, 
                        ),
                        array(
                        \'taxonomy\' => APP_TAX_STORE,
                        \'field\'    => \'slug\',
                        \'terms\'    => $term->slug,
                        \'operator\' => \'NOT IN\',
                         ),
                    ),  
                );
                $query = new WP_Query( $args );
                while ( $query->have_posts() ) :
                    $query->the_post();
                    get_template_part( \'loop\', \'coupon\' ); 
                endwhile;
                wp_reset_postdata();
现在我需要告诉循环只显示每个帖子的一个帖子APP_TAX_STORE (分类学)。

我知道我可能需要第二个查询,然后必须进行组合。然而,我在这个论坛上尝试了太多的解决方案,我完全不知所措。

有了这个问题,我可以给每个APP_TAX_STORE

$terms = get_terms(array(\'taxonomy\' => APP_TAX_STORE)); 
foreach ($terms as $term){
    $args = array(
    \'post_type\' => APP_POST_TYPE, 
    \'posts_per_page\' => \'1\',
    \'order_by\' => \'date\', 
    \'order\' => \'ASC\', 
    \'tax_query\' => array(
        array(
            \'taxonomy\' => APP_TAX_STORE,
            \'field\' => \'slug\',
            \'terms\' => $term->slug, 
            )
        )
    );
    $new_query = new WP_Query ($args);
    if ($new_query->have_posts()) {
        while($new_query->have_posts()){
            $new_query->the_post();
            get_template_part( \'loop\', \'coupon\' ); 

        }
    }

}
现在我需要把这些结合起来!

非常感谢您的帮助!

1 个回复
SO网友:mrben522

假设所有常数都是正确的,它应该是这样的

$terms = get_terms(array(\'taxonomy\' => APP_TAX_STORE));
$tax = get_the_terms($id, \'coupon_category\');
foreach ($terms as $term) {
    $args = array(
        \'post_type\'      => APP_POST_TYPE,
        \'posts_per_page\' => \'1\',
        APP_TAX_TAG      => \'gutschein\',
        \'orderby\'        => array(
            \'meta_value_num\' => \'DESC\',
            \'post_date\'      => \'DESC\',
        ),
        \'tax_query\'      => array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'coupon_category\',
                \'field\'    => \'slug\',
                \'terms\'    => $tax[0]->slug,
            ),
            array(
                \'taxonomy\' => APP_TAX_STORE,
                \'field\'    => \'slug\',
                \'terms\'    => $term->slug,
            ),
        )
    );
    $new_query = new WP_Query ($args);
    if ($new_query->have_posts()) {
        while ($new_query->have_posts()) {
            $new_query->the_post();
            get_template_part(\'loop\', \'coupon\');

        }
    }
}

相关推荐

Custom Taxonomy - fields

我有一个称为项目的自定义分类法,它有一个称为公寓的自定义post类型,两者都有acf自定义字段。我想将分类法创建的项目及其acf字段放在某个页面上,例如项目1、项目2。我创建了taxonomy-projects\\u公寓。php并将其用作模板,用于名为“项目”的页面。$args = array( \'taxonomy\' => \'projects\', \'orderby\' => $orderby, \'show_count