[英]Exclude all sub categories from wp_query
我一直在寻找高低,并回答这个,但我不确定它是否可能!
我有一个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 ); ?>
我将使用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 ); ?>
这只是一个示例,您可能需要稍微修改它以满足您的需求。
所以!
看来我试图做不可能的事。 我无法让这个剧本适合我的生活。 所以我尝试了不同的角度。 我没有排除自定义分类法及其术语,而是决定将我的所有其他术语移到父术语中,而只是调用它。
这是代码,如果有人感兴趣...
<?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(); ?>
我编写了自己的函数,以便从循环中排除子类别帖子,使用上面帖子和其他地方的提示。
在我的主题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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.