WordPress.org

Ready to get started?Download WordPress

Forums

How to exclude a category... (25 posts)

  1. TechGnome
    Moderator
    Posted 7 years ago #

    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.

    -tg

  2. moshu
    Member
    Posted 7 years ago #

    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.

  3. tsguitar
    Member
    Posted 7 years ago #

    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?

  4. TechGnome
    Moderator
    Posted 7 years ago #

    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.)

    -tg

  5. moshu
    Member
    Posted 7 years ago #

    Take a look at this:
    http://wordpress.pastebin.ca/612932

    It has 3 Loops (you, probably, won't need the third one*)
    The "end result" is here:
    http://myprairiegazette.transycan.net/
    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...

  6. TechGnome
    Moderator
    Posted 7 years ago #

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

    -tg

  7. mauropr
    Member
    Posted 7 years ago #

    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 »') ?>

    and

    <?php posts_nav_link('-','« posts mais recentes','posts mais antigos »'); ?>

    Both of them shows the "previous" and "next" links, and creates URLs like:
    feiramoderna.net/blog/page/3
    feiramoderna.net/blog/page/2

    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:
    http://www.feiramoderna.net/blog_template.txt

    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?

  8. FurrTrap
    Member
    Posted 7 years ago #

    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):

    http://www.fidgeting.net/wordpress-plugins/ultimate-category-excluder/

  9. mauropr
    Member
    Posted 7 years ago #

    Thanks for your tip FurrTrap.

    Acctually, Moshu provided the answer! :-)

    http://wordpress.org/support/topic/87020?replies=4#post-444035

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

    I've used:

    <?php $page = (get_query_var('paged'))
    ? get_query_var('paged') : 1;
    query_posts("cat=-25,-35&showposts=10&paged=$page");
    ?>

    And it worked!

    Thanks again!

  10. FurrTrap
    Member
    Posted 7 years ago #

    Just goes to show how valuable these forums really are :)

  11. gspark
    Member
    Posted 6 years ago #

    <?php $page = (get_query_var('paged'))
    ? get_query_var('paged') : 1;
    query_posts("cat=-25,-35&showposts=10&paged=$page");
    ?>

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

  12. mauropr
    Member
    Posted 6 years ago #

    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. :-)

  13. Kafkaesqui

    Posted 6 years ago #

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

    <?php
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=-25,-35&showposts=10&paged=$page");
    ?>

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

    http://php.net/operators.comparison#language.operators.comparison.ternary

  14. Yuchi
    Member
    Posted 6 years ago #

    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 ;)

  15. rat86
    Member
    Posted 6 years ago #

    i have used similar code:

    <?php 
    
    if(!empty($_GET['paged']))
    
    $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

  16. rat86
    Member
    Posted 6 years ago #

    on previous post ther is a error:

    change
    else $paged=4;
    with
    else $paged=1;

    but don't work yet....

  17. rat86
    Member
    Posted 6 years ago #

  18. houseofboyd
    Member
    Posted 6 years ago #

    I'm wanting to do something like this as well. I want to display the titles of my 10 most recent posts in the SIDEBAR. Right now, I'm using:

    <?php get_archives('postbypost', 10); ?>

    But I want to exclude category 88. Any idea of how to do this in the sidebar? Thanks in advance...

  19. houseofboyd
    Member
    Posted 6 years ago #

    Well, I don't know if this was the easy way or the hard way of doing it, but I got this to work with the following code:

    <li id="Recent">
    	<h2>Recently Written</h2>
    <ul>
    <?php $temp_query = $wp_query; query_posts('showposts=10&cat=-88'); ?>
    <?php while (have_posts()) { the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to &ldquo;<?php the_title(); ?>&rdquo;"><?php the_title(); ?></a></li>
    <?php } $wp_query = $temp_query; ?>
    </ul>
    </li>
  20. Greg Rickaby
    Member
    Posted 6 years ago #

    Look for this on your main theme index page:
    <?php if (have_posts()) : ?>

    Insert this right before (or above) it:
    <?php query_posts('cat=-49'); ?>

    Change the category ID to whatever it is you want to exclude.

  21. kylewriter
    Member
    Posted 6 years ago #

    This plug-in, Recent Posts +, worked for me. No coding necessary. It lets you set an option to exclude as many categories/authors as you want.

  22. yonthebeach
    Member
    Posted 6 years ago #

    Is it possible to exclude categories by name an NOT by id?

    thanks
    yavuz

  23. genevaeagles
    Member
    Posted 6 years ago #

    Yuchi,
    your code works great! I had the same problem with the next and previous link and now with your piece of code it's gone...many thanks!

    What exactly is the difference between using

    <?php if(is_home()) {query_post("cat=-39");}?php>

    and what you gave us?

    Stealth Publish is a Plugin from coffee2code which does the same with a custom field.

    But I prefer the method with the hack in the index file. It's clean short and no use for additional Plugin.

  24. Ivovic
    Member
    Posted 6 years ago #

    Lets compare...

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

    firstly, there's supposed to be an 's' in query_posts, secondly, adding the $query_string preserves the pagination information as derived from the URL you're using at the time. The second example is missing a few vital parts, not the least of which is that niggly 's'.

  25. xmailr
    Member
    Posted 6 years ago #

    I think this is related...
    I want to display the 3 most recent excerpts within a specific category
    following a static page post.
    I have a separate template for this page (which only contains content in this category, 7) so I'm coding it in the template just following the post.
    I've found some of the code but am not figuring how to put it together.

    thank you in advance for your help

Topic Closed

This topic has been closed to new replies.

About this Topic