自定义分类和tax_query问题?

时间:2016-01-29 17:26:35

标签: wordpress custom-post-type wp-query custom-taxonomy

我正在进行插件开发,我的插件名称为plugindev。我有一个名为team的自定义帖子类型。我有一个自定义分类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时,我没有收到任何帖子。 这是我的代码

<?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它完美无缺。我做了很多谷歌但发现没有合适的结果。解决这个问题的任何解决方案。任何帮助都将受到高度赞赏

2 个答案:

答案 0 :(得分:1)

register_taxonomy()

  

$ taxonomy(string)(必需)分类法的名称。名字应该   只包含小写字母和下划线字符,而不是   长度超过32个字符(数据库结构限制)。

将您的分类名称从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;
        ),
    ),
);

答案 1 :(得分:0)

WP_Query($ nivelquery),循环现在将打印使用我的自定义分类法以$术语注册的每个帖子,并按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);