Reputation: 452
I appreciate there are millions of wp_query infinite loop questions on here and i have looked through and tried to find the answer but so far nothing seems to quite fit!
The wp_query I'm trying to write should be dead simple but I'm obviously missing something in my code which is;
$args = array ('cat' => 2893);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
echo '<li>' . the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo '<p>nothing</p>';
}
The above code results in an infinite loop with the titles however when I change the echo line to
echo '<li>' . $the_query->the_title() . '</li>';
which I believe should solve the loop issue, the page loads as far as the query and hangs until the execution timeout is reached.
Any ideas on what the problem could be?
Upvotes: 0
Views: 317
Reputation: 6650
I think you forget to add the_post()
Iterate the post index in the loop.
<?php if ( $the_query->have_posts() ) { the_post();
https://developer.wordpress.org/reference/functions/the_post/
Upvotes: 1