WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude category from get_posts? (6 posts)

  1. ThreeVisual
    Member
    Posted 6 years ago #

    Is there a way to exclude a category from the get_posts function?

    On my index page (www.daleanthony.com) I have the 'previous posts' block but I don't want it to show any posts from the 'portfolio' category or category '8' is there any way of doing this?

    Cheers.
    Dale.

  2. MichaelH
    Member
    Posted 6 years ago #

    Don't believe you are going to get there with get_posts.

    Consider using the template tag, query_posts(), instead.

    Assuming the category you want to exclude is 8.

    <?php
    query_posts("cat=-8&showposts=10");
    while (have_posts()) : the_post();
    ?>
    ...
    ?>
    <?php endwhile; ?>
  3. ThreeVisual
    Member
    Posted 6 years ago #

    Perfect, thank you!

  4. 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 from last post added.

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

  5. stonegroper
    Member
    Posted 5 years ago #

    For me get_posts() worked fine for a simple (non-paged) list. I just put a - before the category numbers I wanted to exclude, something like:

    <?php
    $postslist = get_posts('category=-39,-15&numberposts=-1&order=ASC&orderby=title');

    foreach ($postslist as $post) :
    setup_postdata($post);
    ?>

    <?php endforeach; ?>

    My example lists all posts not in category 39 or in category 15, sorted by title.

    To sort by date, most recent first, I use something like 'category=-39,-15&orderby=date&order=DESC'.

  6. stonegroper
    Member
    Posted 5 years ago #

    Try that again:

    <?php
    $postslist = get_posts('category=-39,-15&numberposts=-1&order=ASC&orderby=title');
    foreach ($postslist as $post) :
    setup_postdata($post);
     ?>
     <ul>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to '<?php the_title(); ?>'"><?php the_title(); ?></a>  (<?php the_date(); ?>)</li>
    </ul>
    <?php endforeach; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.