MultiDev
MultiDev

Reputation: 10649

Wordpress: Get recent posts, exclude current post

I am trying to get recent posts, only in the category of the current post, while excluding the current post, but I can't get it to work:

$curr_cat = get_the_category();

$args = array( 'numberposts' => '10', 'post_status' => 'publish', 'category' => $curr_cat['0']->cat_ID, 'exclude' => $post->ID );
$recent_posts = wp_get_recent_posts( $args );

It's just showing the current post over and over again.

Upvotes: 1

Views: 2368

Answers (2)

Tomas Ramirez Sarduy
Tomas Ramirez Sarduy

Reputation: 17471

Your code is fine, be sure that you are echoing your posts like:

foreach( $recent_posts as $recent ){
    echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
}

If you are doing like above, be sure that $post is set and you are in a category archive or single post file.

Upvotes: 0

Ram Sharma
Ram Sharma

Reputation: 8809

John, you can try something like code below, I don't know your case but it works for me in one of my project

$args = array ('category__in' =>  $curr_cat['0']->cat_ID, 'posts_per_page' => 10, 'post__not_in' => array( $post->ID ) );

Upvotes: 2

Related Questions