I have posts that can be both in category ‘5’ and ‘152’ at the same time.
In right sidebar I have to retrieve posts from category ‘5’ but excluding category ‘152’ from that post listing.
I HAVE A WORKING SOLUTION !!!!!
– now you wonder why am I here then, right? !!! 🙂
HERE IS MY DILLEMA:
1) Here is the code of a working solution:
<ul> <?php $lastposts = get_posts('numberposts=7&cat=5&category__not_in=152'); foreach($lastposts as $post) : setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>" target="_blank"> <?php the_title(); ?></a> </li> <?php endforeach; ?> </ul>
2) Here is the code that was NOT working, it was displaying posts also from cat=’152′ despite of the exclude ‘cat__not_in’ :
<ul> <?php $args=array( 'cat' => 5, 'cat__not_in' => array(152), 'post_type' => 'post', 'post_status' => 'publish', 'order' => 'DESC', 'posts_per_page' => 5 ); query_posts( $args ); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a> </li> <?php endwhile; else: ?> <li><?php _e('No other news.'); ?></li> <?php endif; ?> </ul>
Please, for a benefit of a future projects 🙂 , can a good souls that exists on this forum explain to me WHY OH WHY the method 2) with $args in array won’t work in my case?
It was just a lucky coincidence that I tried the working solution.
But some bug in me won’t let me sleep well till I find a reason why.
I need my beauty sleep :-/
Your explanation will help me and many other nonsleeping wordpressers, I am sure … thank you, thank you, thank you 😉
- The topic ‘Post in multiple categories- get posts excluding one cat’ is closed to new replies.