admin管理员组文章数量:1410689
I have posts loaded from taxonomy, but I'm trying to get the data of another, that's also attached to the same post. In the code below, the main focus is getting $location_slug
. Knowing how to get that I think I'd know how to get more data out of the second taxonomy.
// 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>';
}
I have posts loaded from taxonomy, but I'm trying to get the data of another, that's also attached to the same post. In the code below, the main focus is getting $location_slug
. Knowing how to get that I think I'd know how to get more data out of the second taxonomy.
// 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>';
}
Share
Improve this question
asked Jan 24, 2020 at 16:26
Darren BachanDarren Bachan
3752 gold badges7 silver badges17 bronze badges
1 Answer
Reset to default 0First, get all term slugs from the custom example taxonomy space by current post 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;
}
}
You should specify the logical relationship between each inner taxonomy array when there is more than one.
The relation key in the array describes the relationship. Possible values are OR and AND.
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'department_categories',
'field' => 'slug',
'terms' => $course_terms,
),
array(
'taxonomy' => 'space',
'field' => 'slug',
'terms' => $space_terms,
),
),
本文标签: custom taxonomyhow do you pull data from two taxonomies
版权声明:本文标题:custom taxonomy - how do you pull data from two taxonomies? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744807091a2626212.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论