I am looking to exclude a category in WordPress. I am referring to the WordPress Codex page on wp_query
but I am having the following problem:
- I want to exclude posts from category 1,
- but if a post is in category 1 and category 2 (or any other category
other than 1), then I still want it to show.
So, I only want posts to be excluded if they’re exclusively in category 1.
This can be done by getting an array of category ids you want to show. We can use get_terms() for this:
Now you can use this to create your secondary query with WP_Query by using the Category Parameter
category__in
like this:If you for example want to do this just for your home page, than there is no need for a custom secondary query, you can achieve it by hooking into the pre_get_posts action like this: