【问题标题】:Custom Taxonomy and tax_query Issue?自定义分类法和 tax_query 问题?
【发布时间】:2016-01-29 17:26:35
【问题描述】:

我正在从事插件开发,我的插件名称是 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,它可以完美运行。我做了很多谷歌,但没有找到合适的结果。解决这个问题的任何解决方案。任何帮助将不胜感激

【问题讨论】:

  • post-status 不应该是 post_status?
  • 顺便说一句,publish 是 post_status 的默认值,所以不需要添加它
  • @ThemesCreator 我现在删除了'post-status'=&gt;'publish',但它对结果没有影响
  • 你有没有测试变量$category_name的值是否正确?
  • @ThemesCreator ya 当我使用&lt;?php echo $category_name = $get_categories[$i]-&gt;name; ?&gt; 时,它给出了名称,因为有两个名称..

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


【解决方案1】:

register_taxonomy()

$taxonomy (string) (required) 分类的名称。名字应该 只包含小写字母和下划线字符,不包含 长度超过 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;
        ),
    ),
);

【讨论】:

  • 它对我有用,但我不知道为什么这段代码没有运行 'tax_query' =&gt; array(array( 'taxonomy' =&gt; 'team_category', 'field' =&gt; 'slug', 'terms' =&gt;$category_name)) 即使我已将 Team_Category 更改为 team_category
  • 我编辑了我的答案,你定义了税'field' =&gt; 'slug',但你似乎在添加分类名称'terms' =&gt;$category_name的值
  • 在我的情况下应该是这样的$args = array( 'post_type' =&gt; 'team', 'tax_query' =&gt; array( array( 'taxonomy' =&gt; 'team_category', 'field' =&gt; 'slug', 'terms' =&gt; $get_categories, //you need to use slug $mytax-&gt;slug; ), ), ); 但它给了我错误
  • 您的$get_categories 是基于此$category_name = $get_categories[$i]-&gt;name; 的类别名称,您可以简单地将此'field' =&gt; 'slug', 更改为'field' =&gt; 'name', 以使用名称而不是slug,或者如果您更喜欢使用slug,您可以将$get_categories[$i]-&gt;name; 更改为$get_categories[$i]-&gt;slug; 任何你在'field' 上定义的东西,这是你必须提供的
【解决方案2】:

WP_Query($nivelquery) 现在循环将打印使用我的自定义分类法在 $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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多