WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


Hey guys, I have this function and I need to exclude 2 categories: 81 and 82. Can You help? :)


function has_related_same_cat_posts(){
global $post;
$categories = get_the_category($post->ID);

if ($categories) {
    $category_ids = array();
    foreach($categories as $individual_category) 
        $category_ids[] = $individual_category->term_id;

    'category__in' => $category_ids,
    'post__not_in' => array($post->ID),
    'posts_per_page'=> 4, // Number of related posts that will be shown.
    'orderby' => 'rand',


    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) {
        $return = true;
    } else {
        $return = false;


return $return;
share|improve this question
foreach ($categories as $individual_category) {        
    if( $individual_category->term_id == 81 || $individual_category->term_id == 82 ) 
    $category_ids[] = $individual_category->term_id; 


the above replaces:

foreach($categories as $individual_category) 
        $category_ids[] = $individual_category->term_id;

i.e. the code jumps over the to be excluded categories.

share|improve this answer
Please add some explanation to the code. – ialocin Nov 29 '13 at 12:14
@ialocin explanation added. – Michael Nov 29 '13 at 14:59
Perfect, you answer was on the low quality queue, I guess it was because of the missing explanation. – ialocin Nov 29 '13 at 17:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.