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

时间:2017-03-20 09:58:33

标签: 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',
            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仅包含Main类别(1),并且没有任何Child或Grandchild类别。
  • Post具有Main类别(1)并且具有Child类别(2)并且没有任何Grandchild类别。
  • Post有Main类别(1),有Child类别(2),也有Grandchild类别(3)。

有可能吗?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

/** 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().
    }