I am facing a problem with the loop inside the page template. Here is the code.

/* Template Name: Blog-Template */


$args = [
 'post_type' => 'post',
 'posts_per_page' => 1,

$queryP = new WP_Query( $args );

if ($queryP->have_posts()) {
    while ( $queryP->have_posts() ) : $queryP->the_post();

            the_title( '<h1>', '</h1>' );



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.

  • You dont have if ( $query->have_posts() ) condition before your while loop. Try to add this and then check again if it's working – MMPL1 Sep 1 at 13:49
  • you can check the code again, no query variable used – Raashid Din Sep 1 at 13:50
  • Normally loops have an 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 with pre_get_posts so it returned what you wanted? – Tom J Nowell Sep 1 at 13:50
  • Also, the default post type is post and the default status is publish so you don't need to put those in your query. It also needs too do cleanup afterwards – Tom J Nowell Sep 1 at 13:51
  • i added if statement but still not working – Raashid Din Sep 1 at 13:56

From https://wordpress.org/support/article/creating-a-static-front-page/,

Posts Page: (if not already created) create an empty page. Give it a Title that will be used on top of your posts list. This step is mandatory as you are modifying the WordPress default setting. Any other content other than Title will no be displayed at all on this specific page.

So, When you set it as Posts page, Your loop is ignored.

You can set it as static front page (Homepage) or a normal page with custom page template.

| improve this answer | |
  • I have to add a custom loop for the blog. If I change the loop for the blog it will change everywhere. so this doesn't solve the question – Raashid Din Sep 7 at 6:45

It solved automatically. I think the WordPress version 5.5.1 is full of bugs. Thanks, everyone for your support.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.