Show posts, but hide current post


I have a single.php page, where it shows the post data.
At the bottom of the page I want to display all other posts expect the current post.
But I didn’t manage to achieve that. Could you please let me know what I’m doing wrong?

That’s my single.php

<?php get_header(); ?>

<section class="bg-black-500 pb-20 pt-48 px-6 relative sm:px-12 xl:px-20"> 
    <img src="<?php echo get_template_directory_uri(); ?>/assets/images/bg-line.alt1.png" class="absolute bottom-0 h-full right-0 z-0"/>
    <div class="relative z-10 lg:w-1/2"> 
        <div class="flex items-center mb-8">
            <h1 class="font-bold pr-10 text-4xl text-white tracking-wider uppercase"><?php the_title(); ?></h1>
            <img src="<?php echo get_template_directory_uri(); ?>/assets/images/dot-line.png" class="mt-2">
        <p class="font-light text-white text-xl"><?php echo get_the_excerpt(); ?></p> 
    <?php the_content(); ?>
<section class="bg-white px-6 py-10 relative sm:px-12 xl:px-20 xl:py-16"> 
    <img src="<?php echo get_template_directory_uri(); ?>/assets/images/bg-line.png" class="absolute bottom-0 h-full right-16 z-0"/>
    <div class="flex items-center relative z-10"> 
        <h2 class="font-bold pr-10 text-4xl text-black-800 tracking-wider uppercase"><?php _e( 'Autres SERVICES', 'solid_design' ); ?></h2>
        <img src="<?php echo get_template_directory_uri(); ?>/assets/images/bg-line.alt.png" class="mt-2"> 
<section class="text-white"> 
    <div class="mx-auto relative"> 
            $post_query_args = array(
                'post__not_in' => PG_Helper_v2::getShownPosts(),
                'post_type' => 'post',
                'nopaging' => true,
                'order' => 'ASC',
                'orderby' => 'date'
        <?php $post_query = new WP_Query( $post_query_args ); ?>
        <?php if ( $post_query->have_posts() ) : ?>
            <div class="flex flex-wrap">
                <?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
                    <?php PG_Helper_v2::rememberShownPost(); ?>
                    <div id="post-<?php the_ID(); ?>" <?php post_class( 'bg-black-500 w-full sm:w-full lg:w-1/5' ); ?>><a href="<?php echo esc_url( get_permalink() ); ?>"><div class="flex h-28 items-center justify-center px-6"> 
                                <h3 class="font-bold mb-2 text-3xl text-center text-white"><?php the_title(); ?></h3> 
                            </div><div class="h-96 overflow-hidden">
                                <?php echo PG_Image::getPostImage( null, 'large', array(
                                        'class' => 'duration-300 ease-in-out h-full hover:opacity-100 hover:scale-110 object-cover opacity-90 w-full'
                                ), 'both', null ) ?>
                <?php endwhile; ?>
                <?php wp_reset_postdata(); ?>
        <?php else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.', 'solid_design' ); ?></p>
        <?php endif; ?> 

<?php get_footer(); ?>

On the link below, you can see that on the bottom the post “Cuisine” is showing, and it shouldn’t because I’m already on the post “Cuisine”.

Thanks for your help,

I suspect part of your problem is that you don’t have a main loop in your single.php template; only the WP_Query that is getting the list of posts. Without that main loop, the wp_query doesn’t realize that it’s running on a post, and therefore the “skip shown posts” argument does nothing.

You basically just need to create a div with a show posts action (using the main loop) inside your site content, then put everything else inside it.

When in doubt, I like to reference the underscores theme since it has minimal styling and is easy to read, but it has all the guts you need for a fully functional theme.

 * The template for displaying all single posts
 * @link
 * @package Peak_Performance_Digital


	<main id="primary" class="site-main">

		while ( have_posts() ) :

			get_template_part( 'template-parts/content', get_post_type() );

					'prev_text' => '<span class="nav-subtitle">' . esc_html__( 'Previous:', 'ppd-us' ) . '</span> <span class="nav-title">%title</span>',
					'next_text' => '<span class="nav-subtitle">' . esc_html__( 'Next:', 'ppd-us' ) . '</span> <span class="nav-title">%title</span>',

			// If comments are open or we have at least one comment, load up the comment template.
			if ( comments_open() || get_comments_number() ) :

		endwhile; // End of the loop.

	</main><!-- #main -->


Hi Adam,

Thanks for the swift reply.
The main loop was indeed what I was missing.
Everything is working just fine now.

1 Like