Support » Themes and Templates » Query posts cat=-x in category.php – works?

  • Hi

    <?php query_posts(‘cat=-3,-7’); ?>

    this code works in category.php?

    Because when I insert this code and enter in
    Doesn’t just list the internet posts and except the posts of the category ID 3 and 7, but, lists the last posts, independent of the category.

    Did I find that very strange, is it lacking something in my code?

    Thank you.

    Full Code:

    <?php query_posts('cat=-3,-7'); ?>
    <?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
    		<a href="<?php the_permalink() ?>" rel="bookmark" title="Article: <?php the_title(); ?>"><?php the_title(); ?></a><br />
    		<?php the_excerpt(); ?>
    	<?php endwhile; ?>
    <?php else : ?>
    	'0' posts in the category</h2>
    <?php endif; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • I don’t understand what are you trying to do.

    category.php is used by WP to display a list of posts in ONE category – when you click on that category’s name (e.g. in the sidebar). For that normal, general usage of the template file you do NOT need any trickery with the query_posts.

    So, now instead of posting code that that is not readable anyway… explain WHAT do you want to achieve and in which view?

    Ok. My problem is that the menu (sidebar) of the blog I have left to list only categories parents, and with that, when I click on one, opens the visual file category.php listing the latest posts of the category father and their subcategories. And in this case, in one of them, also displays the subcategories of id 3 and 7, and I wanted to exclude them from the listing.

    Thus the category father of id = 2 which list also subcategories of id 3 to 12, and I want NOT list the posts of the subcat id 3 and 7 which are not relevant because appear at the end of the site (pinneds).

    I hope that now I have explained better what I want to do.


    I can see now… but I don’t think you can use the “minus exclude” twice. Al the examples I have ever seen were using it to exclude one category only – like: -3.

    In the page of documentation of the wordpress on query posts:

    You can also add some more categories to the exclude-list(tested with WP 2.1.2):

    if (is_home()) {

    Had that code tested with the version 2.1.2 and as I am using the last 2.3.1 I thought was compatible.

    But when I place an only category him until it excludes in the exhibition (e.g. -3), but stops filtering for the posts of the category and it starts to exhibit all the others.

    Some suggestion to get that what is trying to do?

    Well, that example you quoted is under this title:
    3.1 Exclude Categories From Your Home Page

    I tried your code on a test installation – it doesn’t work.
    As a workaround I would suggest you remove cat 3 and 7 from under the cat 2 as children.

    Thank you for your suggestion, but it doesn’t still work in my case, because the one that I did went to create a special “category” and when I will publish some very good content, I select your category and also the special (that is listed as main prominence in the home of the site). And that same special list (that exhibits last 3 special posts) they appear in the end of each category.

    Therefore, I wanted to filter for not exhibiting the special category in category.php for the same post not to be duplicated in a same page.

    But finally, is there some better way of highlight special posts?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Query posts cat=-x in category.php – works?’ is closed to new replies.