Wordpress: How can I exclude posts in child taxonomies from a custom taxonomy query?

≡放荡痞女 提交于 2019-12-04 17:23:44

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!