| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Testing the number of queries produced by using |
|---|
| 4 | * the post_class() function in custom loops. |
|---|
| 5 | * Please uncomment blocks to show appropriate results. |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * Requiring wp-blog-header.php produces 6 queries. |
|---|
| 11 | * This will be used as a base. |
|---|
| 12 | */ |
|---|
| 13 | require './wp-blog-header.php'; |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /* |
|---|
| 17 | * Basic Loop |
|---|
| 18 | * Looping over queried posts produces 2 additional queries for a total of 8. |
|---|
| 19 | */ |
|---|
| 20 | // while( have_posts() ) { |
|---|
| 21 | // the_post(); |
|---|
| 22 | // the_title( '<h2>', '</h2>' ); |
|---|
| 23 | // } |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /* |
|---|
| 27 | * Add post_class(). |
|---|
| 28 | * Adding post_class() to the loop produces no extra queries. Still at 8. |
|---|
| 29 | */ |
|---|
| 30 | // while( have_posts() ) { |
|---|
| 31 | // the_post(); |
|---|
| 32 | // print '<div'; post_class(); print '>'; |
|---|
| 33 | // the_title( '<h2>', '</h2>' ); |
|---|
| 34 | // print '</div>'; |
|---|
| 35 | // } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /* |
|---|
| 39 | * Custom Query. |
|---|
| 40 | * Quering for attachments adds 5 queries for a total of 11. |
|---|
| 41 | */ |
|---|
| 42 | // $posts = get_posts( array( |
|---|
| 43 | // 'post_type' => 'attachment', |
|---|
| 44 | // 'numberposts' => 5 |
|---|
| 45 | // ) ); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | /* |
|---|
| 49 | * Attachments Loop. |
|---|
| 50 | * This simple loop produces no extra queries. Still at 11. |
|---|
| 51 | */ |
|---|
| 52 | // foreach( (array) $posts as $post ) { |
|---|
| 53 | // setup_postdata( $post ); |
|---|
| 54 | // the_title( '<h2>', '</h2>' ); |
|---|
| 55 | // print '</div>'; |
|---|
| 56 | // } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /* |
|---|
| 60 | * Attachments Loop add post_class(). |
|---|
| 61 | * The addition of post_class() for this custom loop produces |
|---|
| 62 | * 2 queries per iteration: |
|---|
| 63 | * 11 .......... Query for attachments. |
|---|
| 64 | * 13 .......... 1 attachment w/ post_class(). |
|---|
| 65 | * 15 .......... 2 attachments w/ post_class(). |
|---|
| 66 | * 17 .......... 3 attachments w/ post_class(). |
|---|
| 67 | * 19 .......... 4 attachments w/ post_class(). |
|---|
| 68 | * 21 .......... 5 attachments w/ post_class(). |
|---|
| 69 | */ |
|---|
| 70 | // foreach( (array) $posts as $post ) { |
|---|
| 71 | // setup_postdata( $post ); |
|---|
| 72 | // print '<div'; post_class(); print '>'; |
|---|
| 73 | // the_title( '<h2>', '</h2>' ); |
|---|
| 74 | // print '</div>'; |
|---|
| 75 | // } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | /* print total queries. */ |
|---|
| 79 | print get_num_queries() . ' queries.'; timer_stop( 1 ); |
|---|