WordPress

Finding Query Count for a Certain Part in a WordPress Theme

Michael Castilla’s post on modifying individual posts in the loop got me to test the query count between the double loop and single loop with counter method. This is my way to count that query count.

Say the Magic Word

This is how you would display the (current) query count in your theme:

<?php echo get_num_queries(); ?>

This piece of code is usually found inside footer.php, right at the very end of the code to display the total queries needed to display that single page. The secret is that you can place it just about anywhere inside your theme, and it will obediently display the total of query so far.

Also, you can have more than one of these in one go, so you could place one before and one after a certain part of a theme:

<?php echo get_num_queries(); ?>

The Loop here...

<?php echo get_num_queries(); ?>

…and you will have the query count before the loop and after it. Subtract the number and there you go, you have the total of queries invoked for The Loop.

2 Responses to “Finding Query Count for a Certain Part in a WordPress Theme”

  1. Webmaster Weblog » Wordpress: Count SQL Queries

    [...] Source:Playworkplay.com (No Ratings Yet)  Loading … 11 / May / 2008  Wordpress  [...]

  2. Why Book Authors Should Consider WordPress for their Web Presence :: WPLover

    [...] know, it’s article like this, not the uber-geeky stuff like Finding Query Count for a Certain Part in a WordPress Theme that I’d love to see more. Simple, well-written, and easier to understand for the [...]

Leave a Reply

Hello!

My name is Hafiz Rahman. I do standards-based web design and WordPress works. I'm open for new projects, and here's where you can contact me.