WordPress.org

Support

Support » Themes and Templates » [Resolved] Excluding a category in recent posts

[Resolved] Excluding a category in recent posts

  • I have the following code for Recent Posts in my right sidebar, but I want the listed recent posts to EXCLUDE one category–how can i do that??? Thanks!!

    <?php
    $number_recents_posts = 5;
    $c = 0;
    $recent_posts = wp_get_recent_posts(); // grabs 10 recent posts
    if(count($recent_posts) > 0 ) echo '<ol>';
    foreach($recent_posts as $recent_post) {
    if($recent_post['post_status'] == 'publish') {
    $c++;
    if( $c <= $number_recents_posts ) {
    echo '<li><a href="' . get_permalink($recent_post['ID']) . '" title="Read ' .$recent_post['post_title'] . '" >' . $recent_post['post_title'] . '</a>' . '&nbsp;| By&nbsp;';
    $post_author = get_userdata($recent_post['post_author']);
    echo $post_author->display_name . '</li><br />';

    }
    else break;
    }
    }
    if(count($recent_posts) >0 ) echo '</ol>';
    ?>

    </td>
    </tr>

    <?php endif; ?>

Viewing 11 replies - 1 through 11 (of 11 total)
  • Use something like this instead to get 5 recent posts but exclude the events category

    <?php
    $cat_id = get_cat_ID('events');
    $args=array(
      'category__not_in' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 5,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    for some reason that didn’t work….any ideas why not?

    I guess: what, specifically, in your example code do I need to replace with my own category name or ID (I’m not very code at all this, so the more specific, the better!) Thanks!!

    Yes, you need to replace event with the category name that u want to exclude. Go to Posts->Categories in Admin Panel. Or if u mouseover on the category name, you can see cat_ID= and that’s the category id.

    So, let’s say that the category I want to exclude is “archives” and the category id is 9 is it this?:

    <?php
    $cat_id = get_cat_ID('archives');
    $args=array(
    'category__not_in' => $9,

    thanks so much for walking me through this!

    I messed that up, but this should work:

    $cat_id = get_cat_ID('archives');
    $args=array(
    'category__not_in' => array($cat_id),

    or

    $args=array(
    'category__not_in' => array(9),

    wonderful! thank you very very much! One last question…

    here is my blog: http://www.broadrecognition.com/

    I’m trying to get it so that the recent posts are numbered in order….any ideas?? Right now, they’re all numbered 1. for some reason…. here’s my code as it is now:

    <?php
    $cat_id = get_cat_ID('archives');
    $args=array(
    'category__not_in' => array(9),
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <ol><li><p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>&nbsp;| By&nbsp;<?php the_author(); ?></p></li></ol>
    <?php
    endwhile;
    }
    wp_reset_query(); // Restore global post data stomped by the_post().
    ?>

    If you already know the id:

    <?php
    $args=array(
    'category__not_in' => array(9),
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) { ?>
    <ol>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>&nbsp;| By&nbsp;<?php the_author(); ?></p></li>
    <?php endwhile; ?>
    </ol>
    
    <?php }
    wp_reset_query(); // Restore global post data stomped by the_post().
    ?>

    AMAZING!!!! Zeo and MichaelH thank you so so so much!!!!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Resolved] Excluding a category in recent posts’ is closed to new replies.
Skip to toolbar