从定制帖子类型按两个分类排序获取帖子

时间:2015-09-11 作者:Jason

在woocommerce中,我希望产品按品牌(按A-Z排序)和特定的自定义分类法。我有两个分类法product\\u cat和product\\u brand。我编写了一些代码来达到我的目标,但问题是我必须一次又一次地调用WP\\u Query。我尝试的代码:-

$args = array(
    \'orderby\'           => \'name\', 
    \'order\'             => \'ASC\'
    );
    $terms_brand=get_terms(\'product_brand\',$args);
    foreach($terms_brand as $term_b){
    $args = array(
        \'post_type\' => \'product\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'product_cat\',
                \'field\'    => \'term_id\',
                \'terms\'    => $term->term_id,
            )

        ),
        \'product_brand\' => $term_b->slug
    );
    $query=query_posts($args);
    while(have_posts()){
    the_post();
    $terms = get_the_terms( get_the_ID(), \'product_brand\' );
    echo the_title_attribute().\' --- \'.$terms[0]->name.\'<br/>\';

    }
}
我怎样才能不跑呢WP_Query 持续。

1 个回复
SO网友:TheDeadMedic

很接近了,语法有点偏离。我不太清楚您需要查询哪些术语,但这应该是您的论点的结构:

$query = new WP_Query(
    array(
        \'post_type\' => \'product\',
        \'orderby\'   => \'name\',
        \'order\'     => \'ASC\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'product_cat\',
                \'field\'    => \'term_id\',
                \'terms\'    => $ids_of_product_cat_terms,
            ),

            array(
                \'taxonomy\' => \'product_brand\',
                \'field\'    => \'term_id\',
                \'terms\'    => $ids_of_product_brand_terms,
            ),
        ),
    )
);
阅读tax queries from the codex.

相关推荐