Php 通过WP_查询按最深类别获取帖子
我想得到他们最深的类别职位Php 通过WP_查询按最深类别获取帖子,php,mysql,wordpress,Php,Mysql,Wordpress,我想得到他们最深的类别职位 $posts = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'custom_post_type', 'post_status' => 'publish', 'tax_query' => array( 'relation' => 'OR', ar
$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
我只想得到以下职位:
- Post只有主类别(1),没有任何子类别或孙子类别
- Post有主类别(1)和子类别(2),没有任何孙子类别
- Post有主类别(1)、子类别(2)和孙子类别(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().
}
$custom\u category\u args=数组(
'type'=>'custom\u post\u type',
'child_of'=>0,
“父项”=>0,
'orderby'=>'id',
“订单”=>“ASC”,
“hide_empty”=>0,
“层次结构”=>1,
'排除'=>'',
'包括'=>'',
'编号'=>'',
“分类法”=>“自定义类别”,
“pad_计数”=>错误
);
$custom\u categories=get\u categories($custom\u categories\u args);
foreach($custom\u categories作为$custom\u categories){
?>