Support » WP-Advanced » How to exclude a category…

How to exclude a category…

  • TechGnome



    OK, I know how to use get_posts to get posts from a single category. Got that OK…. Now what I want to do is EXCLUDE a category from the listing.

    Here’s what I’m up to, on my page, I want the first post to be the latest post from a particular category – that’s the part I get, no problem. This will be me welcome/site news/upcoming stuff.

    The next thing I want is the last post, UNLESS it’s in the category that is display above. So I want to exclude that category. Otherwise I get the same post listed twice.

    I suppose I could put all my postings, except for the news ones, into a “General” category, then draw from that…. just seems like a kludge….. but probably would work….

    If any one else has any ideas, I’m open to suggestions.


Viewing 15 replies - 1 through 15 (of 24 total)
  • I have a similar setup but in an earlier version of Wp (2.0.x branch) where I am using a home.php template file and not get_post but query_post.
    (Got the code from Kaf in an old topic)

    With query posts you can exclude categories by using “cat=-3” will exclude category ID#3.

    By last post, did you mean next post? So if you have 50 posts, with #50 being the most recent, you want #50 and #49 on your home page, right? Are you just trying to format the latest post differently then the next-to-latest post?




    moshu – Oh cool, so query_post still works? Sweet…. then I have what I need for that (saved from a previous template). I’ll see if that still works for me. If it doesn’t or I have problems, I’ll find a way around it. thanks

    tsguitar – not quite… and at the same time, yes. The top post will not always be the last post period… it could be days, weeks, even months old. Underneath that, will be the latest post. If the welcome post is old, it isn’t a problem, EXCEPT when I then update the site news. I don’t want to show it twice….. only once. To get the site news, when I make the post, it’ll get set to the “Site News” category. Then to get it, I use get_posts, setting categoryid = 2 and numofposts = 1…..

    Now I needed a way to get the latest post on the site, excluding anything that is in the Site News category. If I simply use get_posts and set numofposts = 1, I’ll get the site news again (since it was the latest post). So if the news item was in fact post #50, then yes, I’d want #49…. today, but tomorrow it could be #56, or #70 if I make new posts.

    if you go to the sample site http://tannagh.com/new_site_test/index.html it might help to explain. The section “Welcome” would be the latest news section, followed by the most recent posting.

    Then underneath that, I’ll have two posts which are in a Featured category, one on the left, one on the right.

    Then (again) under *that* will be a listing of the last 5 – 10 posts (again excluding the news category.)


    Take a look at this:

    It has 3 Loops (you, probably, won’t need the third one*)
    The “end result” is here:
    Weekly Buzz – always displays the latest from cat=3.
    On the right (Hot Fresh Topics) the latest posts, minus cat 3; i.e. from any other categories.

    [The third Loop: From the Archives, goes like the second – exclude cat 3 and display X number of posts, BUT offset the number of posts (4) shown in the second Loop]

    *edit: you might…




    moshu – thanks… that’s 75% of what I needed…. I see how I can lay it out now…. thanks a bunch.


    Hi people,

    I had something like that, and get it done with query_posts, but it messed with my navigation throught “previous” and “next” entries.

    I’m using WP 2.2.1, I don’t have any plugins activated, and my site leaves here: http://www.feiramoderna.net

    I have 3 main sessions on the site, each one should display only the posts related to a certain category. So, I created a Page template for each session, and used a script to “filter” the posts, before the loop starts on each one of these Pages.

    <?php query_posts(“cat=-25,-35”); ?>

    It really filtered the content, showing only the posts that doesn’t belong to categories 25 and 35. But, for some reason, it made my navigation through “previous entries” and “next entries” not work at all. Every click on those links take me to a page that looks the same page, although the URL changes.

    I’ve tried 2 ways:

    <?php next_posts_link(‘ยซ posts mais recentes’) ?> – <?php previous_posts_link(‘posts mais antigos ยป’) ?>


    <?php posts_nav_link(‘-‘,’ยซ posts mais recentes’,’posts mais antigos ยป’); ?>

    Both of them shows the “previous” and “next” links, and creates URLs like:

    But the content of each is basically the same as the main page feiramoderna.net/blog

    I put the template here, in case anyone could help:

    This page template is not my index.php file. I use another page template for the Blog session.

    I changed the template to the default Kubrick and it worked. Moved back to my, didn’t work again. So, I assumed that it was something wrong with the template I’ve designer.

    Looking a lit bit further and trying some stuff I’ve tested removing the line <?php query_posts(“cat=-25,-35”); ?> and renaming the template file as index.php (also removing the first lines of the page, where I define the template name). The “previous / next entries” worked fine.

    So, I assume there is a conflict between this query I’m doing to filter the posts of some categories, and the feature for creating the pages with “next” “previous” links.

    I really need to have these 3 sessions, and to exclude some posts from the list on each page, according to the category they belong. But seems that the “next/previous” have some difficulties to understand me! ๐Ÿ™‚

    Anythoughts on how I could fix this?

    Try UCE, you can get it here, still in beta but it works fine if I’m understanding your requirement corrently (I don’t think I’ve quite got a grip on your category switching):


    Thanks for your tip FurrTrap.

    Acctually, Moshu provided the answer! ๐Ÿ™‚


    Instead of using:
    <?php query_posts("cat=-25,35"); ?>

    I’ve used:

    <?php $page = (get_query_var('paged'))
    ? get_query_var('paged') : 1;

    And it worked!

    Thanks again!

    Just goes to show how valuable these forums really are ๐Ÿ™‚

    <?php $page = (get_query_var(‘paged’))
    ? get_query_var(‘paged’) : 1;

    is this real code ? get_query_var(‘paged’) : 1;
    looks like its missing a <?php and a ?>

    gspark, the code is exactly how I’ve posted before.

    I’ve just double-checked at my template, and it is written in the same way it is here. And it’s working just fine, so I assume nothing is missing there. ๐Ÿ™‚

    In case anyone else has the same concerns as gspark, here’s a layout of the code statement that reads better:

    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;

    The first line ($page …) is a ternary operator. If you don’t know what that is:


    there is a easier way to do this. In fact you are reconstucting the post query, adding, cat=-var, but to use less characters you can simply code like this:

    <?php if (is_home()) {
      query_posts($query_string . "&cat=-33");
    } ?>

    easyeasy and does not breake the next/prex links ๐Ÿ˜‰

    i have used similar code:

    $paged = $_GET['paged'];
    else $paged=4;
    query_posts('order=ASC&cat=-403&paged='.$paged') ?>

    but the order ASC or DESC don’t work. i visualized the post from most old. i want visualize the last post of all category excluse 403th.

    i have added also &orderby=date but don’t work. if you are a solution write, thank you

    on previous post ther is a error:

    else $paged=4;
    else $paged=1;

    but don’t work yet….

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘How to exclude a category…’ is closed to new replies.