I have the following code in the footer (I have removed unnecessary html) which I am trying to optimize. My server is suffocating during the day :) If posts_per_page
is set to 1, there are 48 queries per post. If I set it to 18
like I want to, there are 86
queries per post, which for me it seems huge, especially since I have 1500 posts.
Is there a way to optimize this? Why can't WP_Query get the job done in a single query?
$footer_query = new WP_Query(array(
'posts_per_page' => 18,
'orderby' => 'rand',
'post_type' => 'post',
'post_status' => 'publish',
'no_found_rows' => true, // counts posts, remove if pagination require
'update_post_term_cache' => false, // grabs terms, remove if terms required (category, tag...)
// look for post which has featured img
'meta_query' => array(
array(
'key' => '_thumbnail_id',
)
)
));
// LOOP
if ($footer_query->have_posts()) :
while ($footer_query->have_posts()) : $footer_query->the_post();
?>
<a href="<?php the_permalink() ?>">
<?php echo get_the_post_thumbnail( get_the_ID(), 'small_thumbnail', array('class' => 'img-responsive') ); ?>
</a>
<a href="<?php the_permalink() ?>">
<?php the_title(); ?>
<?php echo get_field('subtitle', get_the_ID()); ?>
</a>
<?php
endif;
endwhile;
wp_reset_query();