按分类标准类别查询自定义发布

时间:2011-04-01 作者:Xavier

我希望你今天身体好,

我的问题是,如果我只想通过某种分类法从自定义帖子类型进行查询;

为了保持它的流线型,让我们以下面为例;

Custom Post Type: “产品”

Taxonomy: “类别”

Categories within \'Categories\';

(1)类别1(2)类别2(3)类别3

所以我想从自定义帖子类型查询帖子\'Products\' 和内部\'Category 1\'

如何做到这一点?

下面是我的代码,用于查询自定义帖子类型并尝试查询分类类别。

<?php
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $post_per_page = -1; // -1 shows all posts
    $do_not_show_stickies = 1; // 0 to show stickies
    $args=array(
    \'post_type\' => \'products\',
    \'taxonomy\' => \'category-1\',
    \'paged\' => $paged,
    \'posts_per_page\' => $post_per_page,
    \'order\' => \'ASC\',
    );
    $temp = $wp_query;  // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);
    if( have_posts() ) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

2 个回复
最合适的回答,由SO网友:scribu 整理而成

有三种方法可以做到这一点:

(a)

...
\'category_name\' => \'category-1\'
...
(b)

...

\'taxonomy\' => \'category\',
\'term\' => \'category-1\',

...
c)

...
\'tax_query\' => array(
  array( \'taxonomy\' => \'category\', \'field\' => \'slug\', \'terms\' => array( \'category-1\' ) )
)
...
更多信息:http://codex.wordpress.org/Function_Reference/WP_Query

SO网友:Alvic

含分类法、字段和术语的tax\\u查询数组。其中terms->business是分类->job\\u类别的类别

$args = array(
    \'post_type\' => \'featured_job\',
    \'post_status\' => \'publish\',
    \'posts_per_page\' => 9999999,
    \'orderby\' =>  \'date\',
    \'order\' => \'DES\',

    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'job_category\',
            \'field\'    => \'slug\',
            \'terms\'    =>  \'business\',
        ),
    ),
);

结束

相关推荐