I need some kind of help with wp_query. Am I doing something wrong? I am getting a lot of mySQL queries, while building a new theme.

My WP_Query is:

 $args = array(
   'post_type' => 'items',
   'order' => 'ASC',
'posts_per_page' => 4,
 ); ?>

<?php $lastitems = new WP_Query( $args);
while ( $lastitems -> have_posts() ) : $lastitems -> the_post();?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('thumbnail', array('title' => get_the_title())); ?></a>
    Posted: <?php the_time('d M, Y'); ?>  <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"></a>
                                    <?php endwhile; ?>

Link to pastie.

For this WP_query I'm receiving 5-7 mySQL queries per retrieved info about post.

For example, if i put posts_per_page' => 1 i will get 15 mySQL queries, if i put posts_per_page' => 2 i will get 21 mySQL queries, for 3 is 28, for 10 posts it will be around 60-70 queries just for listings and about 90 mySQL queries in total, etc..

I think that's a little too much.

Also, get_posts uses the same wp_query, so it's not helping. Do you have any idea how to minimize number of mySQL queries, or how to fix my wp_query if I wrote it wrong.



1 Answer 1


There is nothing wrong with your query, this is normal for WordPress.

What is typically more important is the "total query time". A well written theme with over 100 queries will often ring in under 20 milliseconds, that is 4-20 times faster than the blink of an eye. With a caching plugin you can can reduce this a great deal further.

If you really want to optimize I suggest you turn on debugging (with debug bar) and check each SQL query to see if there are any loose ends you can remove, especially of you have a lot of sidebars/widgets and multiple loops.

  • Thanks for answering, Wyck. Yes, everything looks Ok, but at the end i get ~120 mySQL queries. it's this WP_query shown above x4 (4 different places + some banners). At the end without caching i get: <!-- 126 queries. 0.286 seconds --> I have configured mySQL caching, and of course it's a lot faster, just trying to minimize number of reqs. Profiling is my next stop.
    – Dusan
    Commented Nov 13, 2012 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.