如何从两个分类中提取数据?

时间:2020-01-24 作者:Darren Bachan

我有从分类法加载的帖子,但我正在尝试获取另一个帖子的数据,这也附加到同一个帖子上。在下面的代码中,主要关注点是$location_slug. 如果知道如何获取这些数据,我想我会知道如何从第二种分类法中获取更多数据。

// args for the term_query
    $sub_taxonomy = \'department_categories\';
    $tax_args = array(
        \'taxonomy\'               => $sub_taxonomy,
        \'orderby\'                => \'menu_order\',
        \'order\'                  => \'ASC\',
        \'hide_empty\'             => false,
    );

$the_query = new WP_Term_Query($tax_args );

// loop through the terms returned
foreach($the_query->get_terms() as $term){

    $term_id = $term->term_id;
    $term_name = $term->name;
    $term_slug = $term->slug;

    // args for the post query
    $post_type = \'career\';

    $tax_query = array(
        array(
            \'taxonomy\' => $sub_taxonomy,
            \'field\'    => \'id\',
            \'terms\'    => $term_id,
            // \'operator\' => \'IN\',
        )
    );
    $post_args = array(
        \'posts_per_page\' => -1,
        \'orderby\' => \'name\',
        \'order\' => \'asc\',
        \'post_type\' => $post_type,
        \'tax_query\' => $tax_query
    );

    $primary_taxonomy = \'location_categories\';
    $location = get_term( $term_id, $primary_taxonomy);
    $location_slug = $location->slug;

    query_posts( $post_args );

    $html_out .= \'<h6>\' . $term_name . \'</h6>\';

    $html_out .= \'<div data-id="\' . $location_slug . \'" class="tab-pane fade in">\';
            $html_out .= \'<div>\';
                $html_out .= \'<div class="uncode_text_column">\';

                // lopp through the posts
                if (have_posts()) : while (have_posts()) : the_post();

                    $product_title = get_the_title();
                    $product_link = get_the_permalink();

                    $html_out .= $product_title;

                endwhile; else: endif;

                $html_out .= \'</div>\';
            $html_out .= \'</div>\';
        $html_out .= \'</div>\';

}

1 个回复
SO网友:Nuno Sarmento

首先,从自定义示例分类中获取所有术语段塞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;
 }
}

当存在多个内部分类法数组时,应指定每个内部分类法数组之间的逻辑关系。

数组中的关系键描述关系。可能的值为或和。

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

相关推荐

one post per term taxonomy

我再次需要一些帮助。我使用此查询:$tax = get_the_terms($id, \'coupon_category\'); $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; query_posts( array( \'post_type\' => APP_PO