0

I'm trying to exclude the current custom post that is viewed from a list. I've tried with exclude but it still displays it.

Here is the code I have on single-works.php:

<?php
$currentID = get_the_ID();
$recent = new WP_Query( 'post_type=works&exclude=' . $currentID . '&showposts=5' );
while ($recent->have_posts()) : $recent->the_post();
?>

  <li><?php the_title(); ?>

<?php endwhile; ?>

Any idea what I'm doing wrong and how can I make it work?

1 Answer 1

3

Refer to the WP_Query Codex page for a full list and explanation of parameters. The one you want in this case is post__not_in.

$recent = new WP_Query(
    array(
        'post_type' => 'works'
        'posts_per_page' => 5,
        'post__not_in' => array( $currentID )
    )
);
2
  • Also showposts is depreciated and should only use posts_per_page!
    – Sumit
    Commented Mar 29, 2016 at 16:42
  • 1
    @Sumit showposts is NOT depreciated. The documentation is incorrect. showposts is however replaced by posts_per_page. showposts is converted to posts_per_page inside WP_Query. Commented Mar 29, 2016 at 19:18

Your Answer

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

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