droe9376 2017-03-20 09:58
浏览 19

使用WP_Query获取最深层类别的帖子

I would like to get posts by their deepest categories.

$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)
            )
        )
    )
);

For example:

(Main term id) = 1
(Child term id) = 2
(Grandchild term id) = 3

I would like to get only the following posts:

  • Post has only the Main category (1) and doesn't have any Child or Grandchild category.
  • Post has the Main category (1) and has the Child category (2) and doesn't have any Grandchild category .
  • Post has the Main category (1), has the Child category (2) and has the Grandchild category (3) too.

Is it possible? Thank you for your help!

  • 写回答

1条回答 默认 最新

  • doumaojin4008 2017-03-20 11:04
    关注
    /** 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().
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器