問題描述

我沒有得到自定義分類法 (fabric_building_types) 的帖子。我正在獲取 cat_id 和 cat-> 名稱,但不能得到帖子。

$args = array(
    'type'                     => 'post',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'fabric_building_types',
    'pad_counts'               => false

    );
$categories = get_categories( $args );
foreach ( $categories as $cat ) {

// here's my code for getting the posts for custom post type

$posts_array = get_posts(
                        array( 'showposts' => -1,
                            'post_type' => 'fabric_building',
                            'tax_query' => array(
                                array(
                                'taxonomy' => 'fabric_building_types',
                                'field' => $cat->cat_ID,
                                'terms' => $cat->name,
                                )
                            )
                        )
                    );
    print_r( $posts_array );
 }

任何人都可以幫助我… 謝謝提前

最佳解決方案

您的税務查詢不正確,field 應該是您要查詢的字段:term_idnameslug

$posts_array = get_posts(
    array(
        'posts_per_page' => -1,
        'post_type' => 'fabric_building',
        'tax_query' => array(
            array(
                'taxonomy' => 'fabric_building_types',
                'field' => 'term_id',
                'terms' => $cat->term_id,
            )
        )
    )
);

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。