如何在查询中模拟Taxonomy__in?

时间:2013-01-16 作者:Marius

如何设置tax\\u查询以获得“category\\u in”=>array()之类的结果?

具体地说,我想展示所有具有城市分类法术语的帖子,其中一个id是:$cities = array(23,34,45,56);

这是我目前使用的代码。

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

$args = array(
    \'meta_query\' => array(
    array(
            \'key\' => \'mar_bedrooms\',
            \'value\' => $bedroomss,
            \'compare\' => \'IN\'
        ),
    array(
            \'key\' => \'mar_property_for\',
            \'value\' => $property_forss,
            \'compare\' => \'IN\'
        ),
    array(
        \'key\' => \'mar_price\',
        \'value\' => array( $price_min, $price_max ),
        \'type\' => \'numeric\',
        \'compare\' => \'BETWEEN\'
        )
    ),
    \'post_status\'=>\'publish\',
    \'post_type\'=>\'post\',
    //\'cat\' => 1,
    \'category__in\'=> $type,
    //\'taxonomy__in\'=> $city,
    \'orderby\'=>\'date\',
    \'order\'=>\'DESC\'
);
query_posts($args);

if ( have_posts() ) {
    the_post();
    $a = 1;
}

2 个回复
SO网友:Derk-Jan

你不能,因为类别和一般分类法的运作方式。类别是一种分类法,因此在查询类别时,我们查询的级别较低。当您查询category__in => array() 它实际上查找什么category_terms 查询和查询所有这些类别的帖子。现在我们可以模拟这种效果。

$terms_in = array(23,34,45,56);
$taxonomy_terms = get_terms( array( \'city\' ), array(
    \'orderby\' => \'none\',
    \'hide_empty\' => 1,
    \'include\' => $terms_in
) );

foreach ( $taxonomy_terms as $term ) :
    $args = array(
        \'taxonomy\' => $term->slug,
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
    );

    $term_name = $term->slug;
    $query = new WP_Query( $args );
    while( $query->has_posts() ) : 
        $query->the_post();
        // DISPLAY HERE
    endwhile;

endforeach;

wp_reset_postdata();
以上代码是针对您的特定问题编辑的。

SO网友:Marius

经过3天的研究,我找到了在中模拟taxonomy\\u的好方法。

      $args = array(
        \'post_type\' => \'post\',
        \'meta_query\' => array(
            array(
                \'key\' => \'mar_bedrooms\',
                \'value\' => $bedroomss,
                \'compare\' => \'IN\'
            ),
            array(
                \'key\' => \'mar_bathrooms\',
                \'value\' => $bathroomses,
                \'compare\' => \'IN\'
            ),
            array(
                \'key\' => \'mar_property_for\',
                \'value\' => $property_forss,
                \'compare\' => \'IN\'
            ),
            array(
                \'key\' => \'mar_price\',
                \'value\' => array( $price_min, $price_max ),
                \'type\' => \'numeric\',
                \'compare\' => \'BETWEEN\'
            )
        ),
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'city\',
                \'field\' => \'id\',
                \'terms\' => $city,  //$city can be also an array
                \'operator\' => \'IN\'
            )
        ),
        \'post_status\'=>\'publish\',
        \'category__in\'=> $type,  //$type can be also an array
        \'orderby\'=>\'date\',
        \'order\'=>\'DESC\'
    );

    $the_query = new WP_Query( $args );
        if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
wp_reset_query();
wp_reset_postdata();

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post