自定义分类法和 tax_query 问题?

Custom Taxonomy and tax_query Issue?

我正在开发插件,我的插件名称是 plugindev。我有一个名为 team 的自定义 post 类型。我有一个自定义分类法 Team_Category正在通过此代码注册

/***************************taxonomy****************************/
add_action( 'init', 'create_team_taxonomies', 0 );
function create_team_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Team_Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Team_Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Team_Categories' ),
        'all_items'         => __( 'All Team_Categories' ),
        'parent_item'       => __( 'Parent Team_Category' ),
        'parent_item_colon' => __( 'Parent Team_Category:' ),
        'edit_item'         => __( 'Edit Team_Category' ),
        'update_item'       => __( 'Update Team_Category' ),
        'add_new_item'      => __( 'Add New Team_Category' ),
        'new_item_name'     => __( 'New Team_Category Name' ),
        'menu_name'         => __( 'Team_Category' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => false,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'Team_Category' ),
    );

    register_taxonomy( 'Team_Category', array( 'team' ), $args );
}
/****************************taxanomy end***********************************/

但是当我在 WP_Query 中使用 tax_query 时,我没有得到任何 post。 这是我的代码

<?php
$taxonomy_name = 'Team_Category';
$get_categories = get_terms($taxonomy_name);
$total_categories = count($get_categories);
// Loop through the obituaries:
for ($i = 0; $i < $total_categories; $i++) {
    ?>
    <div class="row">
        <div class="col-md-4">
            <?php echo $category_name = $get_categories[$i]->name; ?>
        </div>
        <?php
        $args = array(
            'post_type' => 'team',
            'tax_query' => array(
                array(
                    'taxonomy' => 'Team_Category', 
                    'field' => 'slug', 'terms' => $category_name,)
                )
         );

        $query = new WP_Query($args);

        if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            the_title();
        }
        }
        wp_reset_query(); ?>

    </div>

<?php }

它在没有 tax_query 的情况下工作得很好。我做了很多 google 但没有找到合适的 result.Any 解决方案来解决这个问题。任何帮助将不胜感激

register_taxonomy()

$taxonomy (string) (required) The name of the taxonomy. Name should only contain lowercase letters and the underscore character, and not be more than 32 characters long (database structure restriction).

将您的分类法名称从 Team_Category 更改为 team_category

你应该可以像这样使用参数

$arg = array(
    'post_type' => 'team', 
    'taxonomy' => 'team_category',
    'term' => 'term_name',
);

//using tax_query
$mytax = get_terms('your_taxonomy');
$arg = array(
    'post_type' => 'team', 
    'tax_query' => array(
        array(
            'taxonomy' => 'team_category',
            'field'    => 'slug', 
            'terms'    => 'term_slug', //you need to use slug not name $mytax[0]->slug; 

            #or 
            //'field'    => 'name', 
            //'terms'    => 'term_name', //you need to use term name $mytax[0]->name;

            #or 
            //'field'    => 'term_id', 
            //'terms'    => 'term_ID', //you need to use term ID $mytax[0]->term_id;
        ),
    ),
);

WP_Query($nivelquery) 并且循环现在将打印每个 post 使用我的自定义分类在 $terms 中注册,并按 meta_key 'salary' 对它们进行排序.

   $terms = get_terms('Team_Category',
        array(
            'orderby' => 'slug',
            'order' => 'ASC',
            'hide_empty' => 1,
            'fields' => 'ids',

        ));

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'vagas_tipo',
            'field' => 'id',
            'terms' => $terms,
       ),
    ),
    'orderby' => 'meta_value',
    'meta_key' => 'salary',
    'order' => 'DESC'
);

$query = new WP_Query($args);