php - 使用 WP_Query 按最深类别获取帖子

标签 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

我只想收到以下帖子:

  • 帖子只有主类别 (1),没有任何子类别或孙类别。
  • 帖子具有主类别 (1) 和子类别 (2),并且没有任何孙类别。
  • 帖子具有主类别 (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().
    }

关于php - 使用 WP_Query 按最深类别获取帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42900548/

相关文章:

php - where_in 只返回 1 行

php - 显示的不是id,而是与外键连接的表中某个数据的名称。 - 拉维尔 5.2

mysql - Begin 语句的 SQL 错误 #1064

html - 当视频开始播放到 Wordpress 页面时,在 html(或 css)中添加背光效果

Wordpress Feed Creator 名称标签全名而不是名字

php - 从绝对路径下载文件

php - Doctrine 查询生成器语法错误

mysql - Conditional INNER JOIN or LEFT JOIN 基于连接条件

java - 尝试使用准备好的语句删除元组

php - 为什么我的 MySQL 连接每天晚上都会失败?