0

I'm trying to show recent custom posts in single-events.php I can make it work by using get_posts what I don't know how to do is exclude the post that is active so if I'm on 'Event 1' just now using the code below it will show 'Event 1' in the 'Recent Events' section. How do I get around this?

<?php

$args = array( 
'post_type' => 'events', 
);

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?> 

/* EDIT */

I was missing this: 'exclude' => $post->ID

0

The function get_posts() accepts a parameter 'post__not_in', which allows you to specify an array of post IDs to exclude from the query. get_posts() is just a wrapper for WP_Query, so the documentation for this parameter can be found here.

When inside the loop (such as inside single-events.php) you can retrieve the current post's ID with get_the_ID() (codex). So the following should work:

$args = array( 
    'post_type' => 'events', 
    'post__not_in' => array( get_the_ID() ),
);

$myposts = get_posts( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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