从a开始tax_query
要获取帖子,请使用\'categorie\'
您想要的分类术语。假设自定义查询:
$tax_query_args = array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'categorie\',
\'field\' => \'slug\',
\'terms\' => array( \'costa\' )
),
);
$tax_query = new WP_Query( $tax_query_args );
然后,逐步完成该查询,并使用
get_the_terms()
返回
brand
每个帖子的分类术语,并将它们添加到数组中:
$tax_query_brand_terms = array();
if ( $tax_query->have_posts() ) : while ( $tax_query->have_posts() ) : $tax_query->the_post();
$post_brand_terms = get_the_terms( get_the_ID(), \'brand\' );
if ( is_array( $post_brand_terms ) ) {
$tax_query_brand_terms = array_merge( $tax_query_brand_terms, $post_brand_terms );
}
endwhile; endif;
wp_reset_postdata();
现在
$tax_query_brand_terms
数组应填充所有
brand
在指定的
categorie
分类术语。
如果要将其包装在函数中,则允许传递categorie
分类术语作为参数:
function wpse63233_tax_query_brand_terms( $categorie = false ) {
if ( false == $catgorie ) {
return false;
}
// Also, put in some error checking here,
// e.g. comparing $categorie against
// get_terms( \'categorie\' )
// Query \'categorie\' posts
$tax_query_args = array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'categorie\',
\'field\' => \'slug\',
\'terms\' => array( $categorie )
),
);
$tax_query = new WP_Query( $tax_query_args );
// Get \'brand\' terms from all returned posts
$tax_query_brand_terms = array();
if ( $tax_query->have_posts() ) : while ( $tax_query->have_posts() ) : $tax_query->the_post();
$post_brand_terms = get_the_terms( get_the_ID(), \'brand\' );
if ( is_array( $post_brand_terms ) ) {
$tax_query_brand_terms = array_merge( $tax_query_brand_terms, $post_brand_terms );
}
endwhile; endif;
wp_reset_postdata();
// Return the array
return $tax_query_brand_terms;
}