query multiple taxonomies

时间:2014-11-09 作者:user1374796

我有一个显示“类似产品”的功能,即显示共享相同产品的产品products-category 分类术语。这很有效,但我想进一步缩小函数范围,使其更加具体。因此,我想让它看看2种分类法(product-categoryspace) 寻找类似产品。当前标记如下:

    <?php  
    $terms = wp_get_post_terms( $post->ID, \'products-category\' );
    if($terms){
      // post has course_type terms attached
      $course_terms = array();
      foreach ($terms as $term){
       $course_terms[] = $term->slug;
      }

     $original_query = $wp_query;
     $wp_query = null;
     $wp_query = new WP_Query(
        array(
            \'posts_per_page\' => \'4\',
            \'post_type\' => \'regularproducts\',
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'products-category\',
                    \'field\' => \'slug\',
                    \'terms\' => $course_terms, //the taxonomy terms I\'d like to dynamically query
                ),
            ),
            \'orderby\' => \'title\',
            \'order\' => \'ASC\',
        )
    );

    if ( have_posts() ): ?>

    <?php while (have_posts() ) : the_post(); ?> //etc...
所以它目前只关注products-category 类似产品(帖子)的分类法,但我想让它同时考虑这两个方面product-categoryspace 如有可能,展示更具体的类似产品。如有任何建议,将不胜感激!

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

首先,从自定义分类中获取所有术语段塞space 按当前职位ID。

$space_terms = wp_get_post_terms( $post->ID, \'space\' );
if( $space_terms ) {
  $space_terms = array();
  foreach( $space_terms as $term ) {
   $space_terms[] = $term->slug;
  }
}
当存在多个内部分类法数组时,应指定每个内部分类法数组之间的逻辑关系。

这个relation 数组中的键描述关系。可能的值为ORAND.

\'tax_query\' => array(
    \'relation\' => \'OR\',
    array(
        \'taxonomy\' => \'products-category\',
        \'field\'    => \'slug\',
        \'terms\'    => $course_terms,
    ),
    array(
        \'taxonomy\' => \'space\',
        \'field\'    => \'slug\',
        \'terms\'    => $space_terms,
    ),
),

SO网友:Razon Komar Pal

Simply get all terms first: 这里我使用属性自定义帖子类型作为示例

$property_statu_terms = wp_get_post_terms( $post->ID, \'property_status\', array(  \'fields\' => \'ids\' ));
$property_type_terms = wp_get_post_terms( $post->ID, \'property_type\', array(  \'fields\' => \'ids\' ));
$property_city_terms = wp_get_post_terms( $post->ID, \'property_city\', array(  \'fields\' => \'ids\' ));
$property_state_terms = wp_get_post_terms( $post->ID, \'property_state\', array(  \'fields\' => \'ids\' ));
$property_feature_terms = wp_get_post_terms( $post->ID, \'property_feature\', array(  \'fields\' => \'ids\' ));
使用relation [OR] or [AND] 获得你想要的职位。

$query = new WP_Query( array(

    \'post_type\'             => \'property\',
    \'posts_per_page\'        => 3,
    \'orderby\'               => \'rand\',
    \'post__not_in\'          => array( $post->ID ),
    \'ignore_sticky_posts\'   => 1,
    \'tax_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'taxonomy\' => \'property_status\',
            \'field\'    => \'id\',
            \'terms\'    => $property_statu_terms,
        ),
        array(
            \'taxonomy\' => \'property_type\',
            \'field\'    => \'id\',
            \'terms\'    => $property_type_terms,
        ),
        array(
            \'taxonomy\' => \'property_city\',
            \'field\'    => \'id\',
            \'terms\'    => $property_city_terms,
        ),
        array(
            \'taxonomy\' => \'property_state\',
            \'field\'    => \'id\',
            \'terms\'    => $property_state_terms,
        ),
        array(
            \'taxonomy\' => \'property_feature\',
            \'field\'    => \'id\',
            \'terms\'    => $property_feature_terms,
        ),
    ),

));

结束

相关推荐

Separator for multiple terms

我试图展示自定义分类法的术语。我有这个。但是,当我有多个术语时,我的问题是如何分离我的代码:<ul class=\"slides\"> <?php $loop = new WP_Query( array( \'post_type\' => \'member\', \'posts_per_page\' => -1 ) ); ?> <?php while ( $loop->have_posts() ) : $loop->th