3

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="<?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

12

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)
    )
); 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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