通过自定义 post 类型中的类别 ID 获取 post 自定义分类法

Get post by category id in custom post type with custom taxonomy

这是我创建自定义 post 类型及其分类的脚本。它成功解决了 post 按类别 ID 获取问题的问题。在我的类别中,我有 3 个 post,但找不到任何一个 post。

创建代码:

function man_ourteam_custom_init() {
    $man_labels = array(
        'name' => 'Member',
        'singular_name' => 'ourteam',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Member',
        'edit_item' => 'Edit Member',
        'new_item' => 'New Member',
        'all_items' => 'All Member',
        'view_item' => 'View Member',
        'search_items' => 'Search Member',
        'not_found' => 'No Member found',
        'not_found_in_trash' => 'No Member found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Our Team'
    );
    $man_args = array(
        'labels' => $man_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'ourteam'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'thumbnail')     
    );
    register_post_type('ourteam', $man_args);

   register_taxonomy("member-category", array("ourteam"), array("hierarchical" => true, 'show_admin_column' => true,
        "label" => "Member Categories",
        "singular_label" => "Member Categories",
        "rewrite" => array('slug' => 'ourteam')));
}

add_action('init', 'man_ourteam_custom_init');

对于 post 获取代码:

我的类别 ID 是 191,它有 3 个 post。

$ourteam_category_check = '191';

            $niche_ourteam_args = array(
                'post_type' => 'ourteam',
                'cat'   => $ourteam_category_check,                 
                'orderby' => 'post_date',               
                'order' => 'DESC',
                'post_status' => 'publish',
                'meta_query' => array(
                    array(
                        'key' => '_thumbnail_id',
                        'compare' => 'EXISTS'
                    ),
                )
            );          

            $niche_ourteam = new WP_Query($niche_ourteam_args);

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

                /*** My loop code ***/

            endwhile;

您需要添加 'tax_query',而不是 'cat'..

$ourteam_category_check = '191';

    $niche_ourteam_args = array(
            'post_type' => 'ourteam',
           // 'cat'   => $ourteam_category_check,                 
            'orderby' => 'post_date',               
            'order' => 'DESC',
            'post_status' => 'publish',
            'meta_query' => array(
                array(
                    'key' => '_thumbnail_id',
                    'compare' => 'EXISTS'
                ),
            ),
       'tax_query' => array(
            array(
                'taxonomy' => 'member-category',
                'field' => 'term_id',
                'terms'    => $ourteam_category_check
            ),
       ),
   );          

        $niche_ourteam = new WP_Query($niche_ourteam_args);

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

            /*** My loop code ***/

        endwhile;