I have a page template that has the following:
<?php get_template_part( 'include', 'certifications-popular' ); ?>
Within this file, I have two loops. But having the second loop in there seems to break everything (and nothing is output from either of the loops).
Whereas if I remove the second loop, then the lines of <p>TEST</p>
are output just fine:
<h2>Popular standards</h2>
<div class="row">
<div class="col-12">
<?php query_posts('post_type=page&post_parent=27&posts_per_page=-1&orderby=menu_order&order=ASC'); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<p>TEST</p>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
<h3>Other standards</h3>
<div class="row">
<div class="col-12">
<?php query_posts('post_type=page&post_parent=27&posts_per_page=-1&orderby=menu_order&order=ASC'); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<p>TEST</p>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
</div>
Ignore the fact that the queries are the same, I will eventually add some if
statements within the loops to display what I need in each.
I have a page template that has the following:
<?php get_template_part( 'include', 'certifications-popular' ); ?>
Within this file, I have two loops. But having the second loop in there seems to break everything (and nothing is output from either of the loops).
Whereas if I remove the second loop, then the lines of <p>TEST</p>
are output just fine:
<h2>Popular standards</h2>
<div class="row">
<div class="col-12">
<?php query_posts('post_type=page&post_parent=27&posts_per_page=-1&orderby=menu_order&order=ASC'); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<p>TEST</p>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
<h3>Other standards</h3>
<div class="row">
<div class="col-12">
<?php query_posts('post_type=page&post_parent=27&posts_per_page=-1&orderby=menu_order&order=ASC'); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<p>TEST</p>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
</div>
Ignore the fact that the queries are the same, I will eventually add some if
statements within the loops to display what I need in each.
The root problem here is a missing endif;
on the second loop.
But, there are a few changes we can make to improve this and make things work, this loop, will work a lot better:
<h3>Other standards</h3>
<div class="row">
<div class="col-12">
<?php
$query = new WP_Query( [
'post_type' => 'page',
'post_parent' => 27,
'posts_per_page' => 100,
'orderby' => 'menu_order'
'order' => 'ASC'
] );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p>TEST</p>
<?php
}
wp_reset_postdata();
}
?>
</div>
</div>
query_posts
and wp_reset_query
to WP_Query
, pretend query_posts
doesn't exist, it causes more issues than it solves and WP_Query
is superior in every waywp_rest_postdata
inside the if statement after the while loop, now we only cleanup things when there are things to cleanuptax_query
etcposts_per_page
to a high value rather than unlimited, sure you might only have 20 or 70 pages, but what if you import a tonne by accident or business requirements change? It's better to set an unrealistically high number that you know the page and server can handle as the worst case scenario. Fetching more than 100 posts at a time isn't a good idea, if you need more than 100, consider putting a link to a post archive. ->
and =>
syntaxI'd also consider removing the hardcoded post parent number, as the template will break on a site migration using the WP importer, or if that page is deleted and recreated. It also means if you need to set up the site again or locally this template won't work
So instead here are 3 alternatives:
WP_Query()
instead? It's generally encouraged whilequery_posts()
is typically discouraged. – WebElaine Commented May 29, 2020 at 14:27query_posts
, it causes a lot of problems, always use a standardWP_Query
loop instead. In the meantime take a look at your PHP error log to see what the error message was ( also hey Matt! ) – Tom J Nowell ♦ Commented May 29, 2020 at 14:40