I am facing a problem with the loop inside the page template. Here is the code.
<?php
/* Template Name: Blog-Template */
get_header();
$args = [
'post_type' => 'post',
'posts_per_page' => 1,
];
$queryP = new WP_Query( $args );
if ($queryP->have_posts()) {
while ( $queryP->have_posts() ) : $queryP->the_post();
?>
<article>
<?php
the_title( '<h1>', '</h1>' );
the_excerpt();
?>
</article>
<?php
endwhile;
}
get_footer();
If I set this page as a blog page in settings then no problem happens. But when I create a custom loop for this template it doesn't work. It shows nothing just header and footer.
if ( $posts->have_posts() )
check so that they can display a no posts found type message if there are no posts, but the code here doesn't do that, so there's no way to know if it's because it found no posts, or if it found them but didn't display them. Also, is there a reason you discarded the main query and doubled the amount of work the database had to do, instead of modifying the main query withpre_get_posts
so it returned what you wanted?post
and the default status ispublish
so you don't need to put those in your query. It also needs too do cleanup afterwards