如何设置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;
}
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();