WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude Category in Recent Posts Widget (3 posts)

  1. Moodles
    Member
    Posted 4 years ago #

    I am creating a Widget for my sidebar that lists recent posts, but want to exclude one category. I am using the "Executable PHP widget" plugin.

    I have been trying to use the function
    category__not_in
    Yes I have both the underlines after category. It is working but is INcluding ONLY that category... opposite of what the function should be doing. ???

    Based on this thread, http://wordpress.org/support/topic/272893
    I tried setting the values as an array (I am still a PHP beginner), same result. The correct number of posts are displayed, just the category is not excluded.

    <div class="listWidget"><ul>
    <?php
    $noCat= array (
      'post_type' => post,
      'category__not_in' => news,
      'showpost' => 5
      );
    $posts=get_posts('$noCat');
    if ($posts) {
    foreach($posts as $post) {
    setup_postdata($post);
    ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    
    <?php }
    }
    ?></ul></div>

    Am I doing something wrong in my code? Maybe the code doesn't work correctly in the widget? Other? Thanks for any help.

  2. MichaelH
    Member
    Posted 4 years ago #

    Here's another example

    <?php
    $cat_id = get_cat_ID('news');
    $args=array(
      'category__not_in' => array($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() ) {
      echo 'List of 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. Moodles
    Member
    Posted 4 years ago #

    OK thank you so much, MichaelH. That worked. To tweak it to look like my other widgets, this is what I used in the PHP widget:

    <div class="listWidget"><ul><?php
    $cat_id = get_cat_ID('news');
    $args=array(
      'category__not_in' => array($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(); ?>
        <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?></ul></div>

Topic Closed

This topic has been closed to new replies.

About this Topic