WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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

I need an help with this code.

This one works perfectly, but I have this problem

I have one category (A) and 2 subcat (C) & (D)...

so when I watching the post on a category (C) , I would to show only relative in a category (D).

What I Have to add at this code ?? something like "not_in_category > current.. is it possible??

Please help mee :)

<?php $orig_post = $post;
global $post;
$categories = get_the_category();
if ($categories) {
$category_id = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

'category__in' => $category_ids,
'post__not_in' => array($post->ID, $ids),
'posts_per_page'=> 3, // Number of related posts that will be shown.
share|improve this question

There is category__not_in (see Category Parameters), but I am not entirely sure if it will work properly if you try to include and exclude category at the same time.

If you want to skip some category with your current arguments I think it would be better to not include it in $category_ids array.

share|improve this answer
I tryed to put category__not_in => array (sub catet id); category__in => array (cat parent Id), but doesn't work... i don't know really what I have to do – user3128 Feb 24 '11 at 17:55

I did some testing and I give something not quite accurate as I don't manage multiple category / parents but the straight simple example : one category (A) and 2 subcat (C) & (D) so when i'm in (C) I see (D) posts.

Maybe this could help :

function test_cat($post_id) {

global $post;

$this_cats = get_the_category(); // first we find the category we're in

'cat' => $this_cats[0]->parent, // I ask for all posts in parent cat
'category__not_in' => array($this_cats[0]->term_id, $this_cats[0]->parent), // but not in parent cat nor this cat, that leaves only the 'other' cat
'posts_per_page'=> 3, // Number of related posts that will be shown.

$postslist = get_posts( $args ); // calling 

foreach ($postslist as $post_rel) {
// displaying
share|improve this answer

Your Answer


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