1

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

我有一个 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_in, depth=0,parent=0甚至对此进行改编,但没有运气。

有任何想法吗?

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

因为不可能列出将出现在这里的所有类别,因为它们会一直在变化。

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

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

3 回答 3

4

我将使用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 ); ?>

这只是一个示例,您可能需要稍微修改它以适应您的需要。

于 2013-08-01T12:26:40.283 回答
0

所以!

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

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

<?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(); ?>
于 2013-08-01T14:39:54.650 回答
0

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

在我的主题 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-12-08T04:25:40.567 回答