Hide current selected post from the list wordpress

I displayed all the posts in a page.

The selected post want to hide from the list.

Read More

Here is my code, and screenshot.

<?php
global $post;
if(is_category() || is_single()){
foreach(get_the_category() as $category){
$current = $category->cat_ID;
$current_name = $category->cat_name;
$myposts = get_posts(array('category__in' => array($current)));
$myposts = get_posts('numberposts=50&category='.$current); 
    }
}
foreach($myposts as $post) : setup_postdata($post); 
?>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-3">
    <div class="profile-desc">
        <figure class="bgimg" style="background-image:url('<?php echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ); ?>');"></figure>
        <div class="profile-heading"><?php the_title();?></div>
        <div class="profile-history"><?php the_content(); ?>
            <div class="read">
                <a class="more-link" href="<?php the_permalink();?>"><?php _e('Read More');?></a>
            </div>
        </div>
    </div>
</div>
<?php endforeach; ?>
</div>
</div>

Screenshot

enter image description here

Related posts

Leave a Reply

1 comment

  1. <?php
    global $wp_query;
    $cat_ID = get_the_category($post->ID);
    $cat_ID = $cat_ID[0]->cat_ID;
    $this_post = $post->ID;
    query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
    ?>
    

    You will need to change your query. You can add post_not_in argument to it, so it will exclude it.

    It is much easier faster and safer then other approaches. Also it is the recommended approach by the WP codex:
    http://codex.wordpress.org/Class_Reference/WP_Query#Parameters