Problem with ‘post__not_in’

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() ):?>
    <ul>
    <?php
    while ( $newQuery->have_posts() ) : $newQuery->the_post(); ?>
        <li>
            <a title="<?php the_title();?>" href="https://wordpress.stackexchange.com/questions/31256/<?php the_permalink();?>"><?php the_title();?></a>

        </li>
    <?php
    endwhile;?>
    </ul>
<?php        
endif;
?>

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

1 Answer
1

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( 
    array( 
        'posts_per_page' => 5, 
        'orderby' => 'rand', 
        'cat' => $cats[0]->term_id, 
        'post__not_in' => array($exclude_post)
    )
); 

Leave a Comment