WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Excluding a category in recent posts (12 posts)

  1. ARS11
    Member
    Posted 4 years ago #

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

  2. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  3. ARS11
    Member
    Posted 4 years ago #

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

  4. ARS11
    Member
    Posted 4 years ago #

    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!!

  5. Safirul Alredha
    Member
    Posted 4 years ago #

    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.

  6. Safirul Alredha
    Member
    Posted 4 years ago #

  7. ARS11
    Member
    Posted 4 years ago #

    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!

  8. MichaelH
    Member
    Posted 4 years ago #

    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),
  9. ARS11
    Member
    Posted 4 years ago #

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

  10. Safirul Alredha
    Member
    Posted 4 years ago #

    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().
    ?>
  11. Safirul Alredha
    Member
    Posted 4 years ago #


  12. ARS11
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic