WordPress:如何从自定义分类查询中排除子分类中的帖子?
我的 WordPress 主题有一个名为“集合”的自定义分类法。自定义分类是分层的,因此存在子集合。
我有一个名为“书籍”的集合和一个名为“小说”的子集合。有些帖子只在“书籍”中,有些帖子则在“小说”中。我希望“书籍”集合的页面仅显示主“书籍”集合中的帖子,而不是“小说”子集合中的帖子。但默认情况下,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) );
}
}
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) );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来 WP_Query 类 始终包含层次分类法。如果你想解决这个问题,你可以使用他们使用的相同技巧:获取分类项的所有子项,然后获取这些子项中的所有帖子 ID,然后将它们放入
post__not_in
参数中:这将产生一个具有
AND posts.ID IN (1, 2, 3) AND posts.ID NOT IN (2, 3)
的查询,该查询将仅返回 ID 为 1 的帖子。不优雅,但它有效。当然,如果你走这条路,你也可以只传递你想要的帖子 ID,而不告诉查询任何有关分类的信息。
您如何对类别执行此操作? 查询代码似乎也包含子项。
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: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.