The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

exclude category from $wp_query (6 posts)

  1. Dextro
    Posted 11 years ago #

    Is there a way to exclude a category from $wp_query?

    If I use !in_category() in the loop, then the counting of the posts is not correct and posts_nav_link() doesn't work well then.

    If I use query_posts() then the original query is broken.

    So I want the original query but without the posts of 1 category. Is that possible?

  2. Michael Bishop

    Posted 11 years ago #

    If you use query_posts('cat=-3') (where 3 is what ever cat ID you want) you should get the same query simply with out the cat Id you specify, shouldn't you? Where are you using the query_post? Before the loop?

  3. Dextro
    Posted 11 years ago #

    On http://codex.wordpress.org/Template_Tags/query_posts, you can read that WordPress then ignores the other parameters it receives via the URL (such as page number or category)

    I just want the query to use this parameters, but exclude a specific category.

  4. Michael Bishop

    Posted 11 years ago #

    That's exactly what it does. It excludes the category you don't want, but otherwise runs the loop just as it would normally. I've used it before, and it doesn't exclude anything that you don't set in the parameters. I think the key term is "such as". There are quite a bit of parameters than you can pass through it, so the language is vague in the codex. Also see the article is "unfinished".

  5. Joshua Sigar
    Posted 11 years ago #

    Try the following method of excluding category. I believe it preserves all other existing query parameters.


  6. Dextro
    Posted 11 years ago #

    @miklb: query_posts really ignores all the parameters it receives via the url.

    @alphaoide: Thx! That's what I was looking for.

Topic Closed

This topic has been closed to new replies.

About this Topic