Wordpress:如何从自定义分类查询中排除子分类中的帖子? [英] Wordpress: How can I exclude posts in child taxonomies from a custom taxonomy query?

查看:132
本文介绍了Wordpress:如何从自定义分类查询中排除子分类中的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WordPress主题有一个自定义分类法,称为集合".定制分类法是分层的,因此有子集合.

我有一个名为"Books"的收藏夹和一个名为"Novels"的子收藏夹.有些帖子仅在书籍"中,而有些帖子则在小说"中.我希望书籍"集合的页面仅显示主书籍"集合中的帖子,而不显示小说"子集合中的帖子.但是默认情况下,WordPress在分类法查询中的子集合"中包含帖子.

如何从分类查询中排除子级帖子?使用类别很容易,但是似乎没有内置的方法可以使用自定义分类法.


更新: Jan的解决方案效果很好.这是我使用的代码,位于index.php中的循环上方:

// if is taxonomy query for 'collections' taxonomy, modify query so only posts in that collection (not posts in subcollections) are shown.
if (is_tax()) {
 if (get_query_var('collection')) {
  $taxonomy_term_id = $wp_query->queried_object_id;
  $taxonomy = 'collection';
  $unwanted_children = get_term_children($taxonomy_term_id, $taxonomy);
  $unwanted_post_ids = get_objects_in_term($unwanted_children, $taxonomy);

  // merge with original query to preserve pagination, etc.
  query_posts( array_merge( array('post__not_in' => $unwanted_post_ids), $wp_query->query) );
 }
}

解决方案

似乎WP_Query类查询代码似乎也包含子级.

My WordPress theme has a custom taxonomy called "Collections". The custom taxonomy is hierarchical, so there are subcollections.

I have a Collection called "Books" and a sub-collection called "Novels". There are some posts that are just in "Books", and some posts that are in "Novels". I want the page for the "Books" collection to only show posts in the main "Books" collection, not the ones in the "Novels" subcollection. But by default, WordPress includes posts in "subcollections" in the query for a taxonomy.

How do I exclude posts in child terms from my taxonomy query? This is easy with categories, but it seems there is no built in way to do this with custom taxonomies.


Update: Jan's solution worked perfectly. Here is the code I used, placed above the Loop in index.php:

// if is taxonomy query for 'collections' taxonomy, modify query so only posts in that collection (not posts in subcollections) are shown.
if (is_tax()) {
 if (get_query_var('collection')) {
  $taxonomy_term_id = $wp_query->queried_object_id;
  $taxonomy = 'collection';
  $unwanted_children = get_term_children($taxonomy_term_id, $taxonomy);
  $unwanted_post_ids = get_objects_in_term($unwanted_children, $taxonomy);

  // merge with original query to preserve pagination, etc.
  query_posts( array_merge( array('post__not_in' => $unwanted_post_ids), $wp_query->query) );
 }
}

解决方案

It seems the WP_Query class always includes all items of hierarchical taxonomies. If you want to counter this, you can use the same trick they use: get all subitems of your taxonomy item, then get all the post id's in those subitems, and then put them in the post__not_in parameter:

$unwanted_children = get_term_children($taxonomy_term_id, $taxonomy);
$unwanted_post_ids = get_objects_in_term($unwanted_children, $taxonomy);

This will result in a query that has AND posts.ID IN (1, 2, 3) AND posts.ID NOT IN (2, 3), which will return only this post with ID 1. Very unelegant, but it works.

Of course, if you go this route, you could also just pass the post id's you want, and tell the query nothing about the taxonomy.

How do you do this for categories? The query code seems to include children there too.

这篇关于Wordpress:如何从自定义分类查询中排除子分类中的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆