【问题标题】:Exclude all sub categories from wp_query从 wp_query 中排除所有子类别
【发布时间】:2013-08-01 12:12:53
【问题描述】:

我一直在寻找和回答这个问题,但我不确定这是否可能!

我有一个 WP_Query 可以从几乎所有内容中提取帖子,但是,我希望排除特定类别和/或它的所有子类别。

搜索周围的人还没有找到解决方案。

这是我目前的查询:

$args = array(
    'post_type' => 'sell_media_item',
    'cat' => -98,
    'orderby' => 'desc',
    'paged' => $paged,
    'posts_per_page' => 20
); ?>

<?php $loop = new WP_Query( $args ); ?>

我认为仅排除 cat 98 也会获取所有子类别,但显然不是。

我尝试过使用:

category__not_indepth=0parent=0 甚至是an adaptation of this,都没有运气。

有什么想法吗?

[编辑] 我正在使用一个名为 Collections 的自定义分类法,因此将 'collection' =&gt; 'vip' 放入查询中意味着它只会显示此集合。我在想是否有办法扭转这种情况,从而排除收藏?

由于无法列出将出现在此处的所有类别,因为它们会一直在变化。

[编辑 2] 在下面的 cmets 讨论之后,这里是更新的代码。

$ex = array(
    'taxonomy' => 'collection',
    'child_of' => 98,
    'hide_empty' => 0
);
$categories = get_categories($ex);

$categoriesToExclude = array();
foreach ($categories as $category) {
    $categoriesToExclude[] = $category->cat_ID;
}

echo('<pre>'); var_dump($categories);


$args = array(
    'post_type' => 'sell_media_item',
    'category__not_in' => $categoriesToExclude,
    'orderby' => 'desc',
    'paged' => $paged,
    'posts_per_page' => 20
); ?>

<?php echo('<br /><pre>'); var_dump($args); ?>

<?php $loop = new WP_Query( $args ); ?>

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    我会用get_categories() 获取所有子类别的列表,然后根据结果构建一个'cat' 排除数组。

    $args = array('parent' => 98);
    $categories = get_categories($args);
    
    $categoriesToExclude = array();
    foreach ($categories as $category) {
        $categoriesToExclude[] = $category->cat_ID;
    }
    
    
    $args = array(
        'post_type' => 'sell_media_item',
        'category__not_in' => $categoriesToExclude,
        'orderby' => 'desc',
        'paged' => $paged,
        'posts_per_page' => 20
    ); ?>
    
    <?php $loop = new WP_Query( $args ); ?>
    

    这只是一个示例,您可能需要稍作修改以满足您的需要。

    【讨论】:

    • 我的类别是自定义分类法这一事实是否会对此产生任何影响?似乎不起作用:/
    • 你在$categoriesToExclude 数组中得到了什么?也许您需要在get_categories() 中指定类型或其他内容,不确定。请参阅get_categories() 的文档。
    • 是的,刚刚检查了这个,在 $args 中添加 'taxonomy' => 'collection' 似乎没有任何改变
    • 那么get_categories($args) 会返回什么?使用print_r($categories)进行调试。
    • 啊!您的 $categories = get_categories($args) 中缺少分号
    【解决方案2】:

    所以!

    看来我正在尝试做不可能的事情。我无法让这个脚本为我的一生工作。所以我尝试了不同的角度。我没有排除自定义分类法及其术语,而是决定将所有其他术语移动到父术语中,并改为调用它。

    如果有人感兴趣,这里是代码...

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;               
    
    $args = array(
        'post_type' => 'sell_media_item',
        'taxonomy' => 'collection',
        'term' => 'clubs',
        'orderby' => 'desc',
        'paged' => $paged,
        'posts_per_page' => 20
    );
    
    
    $loop = new WP_Query( $args );
    
    if ( $loop->have_posts() ) : while ($loop->have_posts()) : $loop->the_post(); ?>
    

    【讨论】:

      【解决方案3】:

      我使用上述帖子和其他地方的提示编写了自己的函数,以便从循环中排除子类别帖子。

      在我的主题 archive.php 文件中,在循环上方,我列出了子类别(可选):

          <?php
             $current_cat = get_queried_object();
      
             $args = array( 'parent'=>$current_cat->term_id, 'child_of' => $current_cat->term_id, );
              $categories = get_categories( $args );
              foreach($categories as $category) { ?>
      
                 <h2><?php echo $category->name ;?></h2>
                 <p> etc....</p>
            <?php } ?>
      

      在我的 functions.php 文件中,我使用 pre_get_posts 添加了以下自定义函数:

      add_action( 'pre_get_posts', 'main_query_without_subcategory_posts' );
      
      function main_query_without_subcategory_posts( $query ) {
      
      if ( ! is_admin() && $query->is_main_query() ) {
          // Not a query for an admin page.
          // It's the main query for a front end page of your site.
      
          if ( is_category() ) {
      
         //Get the current category
              $current_category = get_queried_object();
              //get the id of the current category
              $current_cat_id = $current_category->term_id;
      
              //find the children of current category
              $cat_args = array( 'parent'=>$current_category->term_id, 'child_of' => $current_category->term_id, );
              $subcategories = get_categories( $cat_args );
      
              //Get a list of subcategory ids, stick a minus sign in front
              $subcat_id = array();         
              foreach($subcategories as $subcategory) { 
                  $subcat_id[] = " -". $subcategory->term_id; 
              }
      
              //join them together as a string with a comma seperator          
              $excludesubcatlist = join(',', $subcat_id);
      
             //If you have multiple parameters, use $query->set multiple times
              $query->set( 'posts_per_page', '10' );
              $query->set( 'cat', ''.$current_cat_id.','.$excludesubcatlist.'' );
            }
          }
        }
      

      然后在 archive.php 的子类别下方,我添加了常规的 WordPress 循环,现在正在由上述函数修改:

      <?php  while (have_posts() ) : the_post(); ?>
           <h2><?php the_title();?></h2>
           <p> etc....</p>
       <?php endwhile;?>
      

      虽然 WordPress 法典说使用“category__in”会从子类别中排除帖子,但这对我不起作用,并且子类别帖子仍在显示。

      https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

      https://developer.wordpress.org/reference/hooks/pre_get_posts/

      【讨论】:

        猜你喜欢
        • 2018-05-17
        • 1970-01-01
        • 2013-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-13
        • 1970-01-01
        • 2017-06-23
        相关资源
        最近更新 更多