I am trying to list posts in a widget I am working on at the moment. However, when I use the get_template_part function in my loop, my site just keeps loading until I get a 500 http error.
$args = array(
'numberposts' => 10,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => $postType,
'suppress_filters' => true
);
$postsQuery = new WP_Query( $args );
if ( $postsQuery->have_posts() ) {
while ( $postsQuery->have_posts() ) {
get_template_part( 'content/content', $postType );
}
/* Restore original Post Data */
wp_reset_postdata();
}
When I comment the get_template_part
out, everything works just fine. Also the loop seems to loop perfectly through the posts. Just the get_template_part
is somewhat problematic. I don't have any idea, how I could figure out what the problem is. Any ideas? Maybe I am using it wrong or something?
Here's the solution:
I was missing the $postsQuery->the_post()
in my loop. Also guilemons tip helped, that I needed a folder called content in the same directory where my php file was with the templates.
the_post
, so it never progresses, it's an infinite loop – Tom J Nowell♦ Mar 9 '18 at 14:11'suppress_filters' => true
is bad for performance, and you can improve performance by adding'post_status' => 'publish'
which simplifies the query, WP adds extra logic for showing your private posts that slows things a little – Tom J Nowell♦ Mar 9 '18 at 14:12