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 am running a custom query below each post, to get other post from its category. Now I want to exclude the current post. This is my Query:

<?php // related_posts();
$exclude_post   = $post->ID;
$cats =  get_the_category();
//$cats[0]->term_id;$cats[1]->term_id; //name
 global $post;
 $newQuery = new WP_Query('posts_per_page=5&orderby=rand&cat='.$cats[0]->term_id.'&post__not_in='.array($exclude_post).''); 
 if ( $newQuery->have_posts() ):?>
    while ( $newQuery->have_posts() ) : $newQuery->the_post(); ?>
            <a title="<?php the_title();?>" href="<?php the_permalink();?>"><?php the_title();?></a>


Now my query shows 0 results. The same, if I set the post which shall be excluded testwise to 1 or so.

What could be the error in my custom query?

Cheers Lars

share|improve this question
up vote 3 down vote accepted

You are trying to supply an array as part of the string query parameter. You could instead just supply the arguments list as an array like this:

$newQuery = new WP_Query( 
        'posts_per_page' => 5, 
        'orderby' => 'rand', 
        'cat' => $cats[0]->term_id, 
        'post__not_in' => array($exclude_post)
share|improve this answer
Thanks man ;) excatly what i needed! – Sagive SEO Mar 16 '12 at 23:10

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.