[英]Get posts by their deepest categories with WP_Query
我想按他们最深的类别获得帖子。
$posts = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'custom_post_type',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'custom_category',
'field' => 'term_id',
'terms' => (Main term id)
),
array(
'taxonomy' => 'custom_category',
'field' => 'term_id',
'terms' => (Child term id)
),
array(
'taxonomy' => 'custom_category',
'field' => 'term_id',
'terms' => (Grandchild term id)
)
)
)
);
例如:
(Main term id) = 1
(Child term id) = 2
(Grandchild term id) = 3
我只想获得以下职位:
可能吗? 谢谢您的帮助!
/** you can try below code **/
$custom_category_args = array(
'type' => 'custom_post_type',
'child_of' => 0,
'parent' => 0,
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'custom_category',
'pad_counts' => false
);
$custom_categories = get_categories( $custom_category_args );
foreach ( $custom_categories as $custom_category ) {
?>
<a href="<?php echo get_term_link(intval($custom_category->term_id), $custom_category->taxonomy);?>"><?php echo $custom_category->name;?><a/>
<?php
$custom_type = 'custom_post_type';
$custom_args=array(
'post_type' => $custom_type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> -1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'custom_category',
'field' => 'id',
'terms' =>$custom_category->term_id
)
),
'orderby' => 'id',
'order' => 'ASC'
);
$custom_my_query = null;
$custom_my_query = new WP_Query($custom_args);
$custom_my_total_count = count($custom_my_query);
if( $custom_my_query->have_posts() )
{
while ($custom_my_query->have_posts()) : $custom_my_query->the_post();
?>
<a href="<?php echo get_permalink();?>"><?php echo get_the_title($post->ID);?></a>
<?php
endwhile;
}
wp_reset_query($custom_my_query); // Restore global post data stomped by the_post().
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.