• Resolved vestaviascott

    (@vestaviascott)


    I need to list all posts in the site which are not in a specific named category (my-blog-title) is the category. What’s the best way to do this?

    I’m thinking if I just add the recent posts widget to the sidebar, then hack my sidebar.php script, just before the widget include, with a get_posts directive that might do it, no?

    Just need some direction and ideas. Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • <?php
    $cat=get_cat_ID('yourcategoryname');
       $args=array(
       'category__not_in' => array($cat),
       'showposts'=>-1,
       'caller_get_posts'=>1
       );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><small><?php the_time('m.d.y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
    endwhile;
    }
    ?>
    Thread Starter vestaviascott

    (@vestaviascott)

    Awesomely fast response Michael! Now I know why you are the moderator. Thanks.

    Just one more thing, I want the exclusion category to be dynamically scripted to match the lowercase of the blog title with “-” replacing the spaces. So if the Blog title is “My Blue Widget”, I want the exclusion category to be “my-blue-widget”…

    Also, I’m using a list item structure, but no biggie…here’s my code…Would you do anything differently?

    <div class="xmenu side"><span>Recent Posts</span>
    	<ul>
    	<?php
    	$mycat = strtolower(get_bloginfo());
    	$mycat2 = str_replace (" ", "-", $mycat);
    
    	$cat=get_cat_ID($mycat2);
    	$args=array('category__not_in' => array($cat),'showposts'=>-1,'caller_get_posts'=>1);
    	$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(); ?>"><?php the_title(); ?></a></li>
    	<?php
    	endwhile;
    	}
    	?>
    	</ul>
    </div>

    Looks good. Thanks.

    Thread Starter vestaviascott

    (@vestaviascott)

    Michael, thanks again for your help. I’ve streamlined the code a bit but for some reason, I’m getting a single post listed that is in the exclude category…
    (the exclude category is named “top-menu” and the slug is also named top-menu, which is being referenced with get_cat_ID?)

    <ul><h3>Recent Posts(not in the top-menu category</h3>
    		<?php
    		global $post;
    		$cat=get_cat_ID('top-menu');
    		$myposts2 = get_posts('category!='.$cat);
    		foreach($myposts2 as $post) :
    		?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endforeach; ?>
    	 </ul>
    Thread Starter vestaviascott

    (@vestaviascott)

    I’ve got $cat and $post declared in the header as well, will these conflict with one another? Particularly since $post is global in scope?

    I’m pretty sure this won’t work

    $myposts2 = get_posts('category!='.$cat);

    See other arguments here:
    query_posts()

    Thread Starter vestaviascott

    (@vestaviascott)

    ALMOST THERE!!! I got it all sorted out, but now I need to exclude all sticky posts…

    <ul><h3>Recent Posts</h3>
    		<?php
    		global $post;
    		$cat=get_cat_ID('top-menu');
    		$myposts2 = get_posts('cat='.-$cat);
    		foreach($myposts2 as $post) :
    		?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endforeach; ?>
    	 </ul>
    Thread Starter vestaviascott

    (@vestaviascott)

    This did it….

    <ul><h3>Recent Posts</h3>
    	<?php
    	global $post;
    	$cat=get_cat_ID('top-menu');
    $myposts2=get_posts(array('post__not_in'=>get_option('sticky_posts'),'cat'=>-$cat));
    		foreach($myposts2 as $post) :
    		?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endforeach; ?>
    	 </ul>

    Phew … 😉

    Good job.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘List all posts not in a named category’ is closed to new replies.