0

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.

  • Are you sure it's not the template that's causing the 500 error? A 500 error is the servers way of saying "Something went wrong, don't know what, look at the log to see what happened", so check your PHP error logs. Also, your loop never calls the_post, so it never progresses, it's an infinite loop – Tom J Nowell Mar 9 '18 at 14:11
  • Also, '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
  • Thanks! I will do that! I solved the problem with guilemons answer. Maybe the 500 is just my servers weird way of saying it doesn't like my way of building loops ;) – TheKidsWantDjent Mar 9 '18 at 14:35
1

The get_template_part() function takes filepath relative to the file it is called in. Do you have a folder called 'content' which contains a file content-posttype.php? is the 'content' folder located in the same folder as this php file you are calling get_template_part() in?

Also, you can modify your while loop as follows:

        while ( $postsQuery->have_posts() ) {
          $postsQuery->the_post();
          get_template_part( 'content/content', $postType );//please check the file path.
        }
1

You're missing $postsQuery->the_post() in your loop. That function moves the loop to the next post, without it you're performing an infinite loop.

while ( $postsQuery->have_posts() ) {
    $postsQuery->the_post();
    get_template_part( 'content/content', $postType );
}

get_template_part() is irrelevant to the issue. I don't know enough about PHP's internals to be sure, but I suspect it's working when you comment it out because a loop with nothing in it isn't executed.

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.