I have a single.php template but it shows all blogs posts. How do I prevent it from showing all posts:
Below is my code:
<?php $args = array('post_type' => 'realweddings');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="post-heading entry-title"><span class="left-hanger"><?php the_title(); ?> </span></h1>
<section class="overview">
<?php the_content(); ?>
</section>
<?php endif;?>
<?php comments_template(); ?>
</article>
<?php endwhile;?>
I think the correct question is, why am I using WP_Query
to construct my loop in single.php. Frankly, I can't answer both.
Your problem is your custom query and the straight forward answer is, delete your custom query. You should never use a custom query in place of the main query. The main query is very specific on templates as it uses the URL to set the arguments in the main query, which is in fact also just a normal WP_Query
Just use the normal loop, that should fix your issue. A custom query is not the way to solve an issue with the main query