• Resolved Moodles

    (@moodles)


    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, https://www.ads-software.com/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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • MichaelH

    (@michaelh)

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

    Thread Starter Moodles

    (@moodles)

    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>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude Category in Recent Posts Widget’ is closed to new replies.