• Hi

    I was wondering if anyone could help me.

    I’ve created a new category on my site and I want to display the posts within a widget instead of with my main reviews. Its going to act as an editorial comments bar, commenting on other news and blogs, with editorial control. I don’t know if I’m approaching it quite the right way, but want to experiment with it this way, as I want it still to ping to twitter and facebook. The site is https://www.jadedeye.com

    Anyway I’m using the Equilibrium theme and the query code that it uses is as follows:

    <?php query_posts(‘cat=ID&showposts=2’); ?>

    What do I need to write in to exclude a category?

    There is also the following codes in two other separate areas and have a feeling that I’m going to have to edit these too?

    <?php query_posts(‘cat=ID&showposts=1&offset=2’); ?>

    and

    <?php query_posts(‘cat=-ID&showposts=8&offset=3’); ?>

    Thanks
    J-P

Viewing 4 replies - 1 through 4 (of 4 total)
  • You should try using category__not_in instead of cat in the query_posts() function. That will allow you to exclude the category, instead of explicitly including it.

    Thread Starter jupperpeep

    (@jupperpeep)

    Hi

    Thank you for your message, so my code should look something like this?

    <?php query_posts(‘category__not_in=> array(4 )&showposts=2’); ?>

    4 being the category number?

    Thanks

    I think it would need to look more like:
    <?php query_posts( 'category__not_in=4&showposts=2' ); ?>
    or
    <?php query_posts( array( 'category__not_in' => array( 4 ), 'showposts' => 2 ) ); ?>

    Or, as indicated in the last block of code you provided in your original post (since you’re only trying to exclude a single category), you could just use:
    <?php query_posts('cat=-4&showposts=2'); ?>

    All of these code snippets should retrieve posts from all of your categories except for the one with an ID of 4; but will only retrieve two posts at a time.

    Thread Starter jupperpeep

    (@jupperpeep)

    Hi

    Apologies for the late reply, thank you for your help, but I’m still having problems making this code work for me as it still show the post in the unwanted area.

    Is there another way of going about this?

    Am I correct in thinking that the default category is 0 and then it goes up regarding IDs?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to exclude a category from a post query’ is closed to new replies.