Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
WordCamp Austin 2012
Chris OlbeksonWordPress ConsultantC3M Digital@Chris_Olbekson on Twitterchris@c3mdigital.comWordCamp Austin 2012
The Query, The WholeQuery, & Nothing But     $the_query
What is the query?
What is the query?     The Loop
What is the query?           The Loop  while( have_posts() ): the_post();    the_content();  endwhile;
What is the query?   WP_Query class
What is the query?   $wp_query object
class WP_Query
$wp_query objectThe WP Class gives the $wp_query objectinformation that defines the currentrequest.
class WP_Query            $wp_query objectThe WP Class gives the $wp_query objectinformation that defines the currentreque...
class WP_Queryclass WP {   function main() {       $this->init();       $this->parse_request();       $this->query_posts()...
class WP_Query                   Properties$query, $query_vars, $posts, $post_count,$current_post, $queried_object_id, etc...
class WP_Query                   Methodsinit(), parse_query(), set( $query_var, $value ),have_posts(), rewind_posts() etc...
class WP_QueryInteracting with WP_Query
class WP_Query      Interacting with WP_QueryBefore the loopAltering the query, filters
class WP_Query      Interacting with WP_QueryDuring the loopConditionals and template tags,secondary queries, get_sidebar(...
How do we Query?
How do we Query?query_posts() alters the main query
How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries
How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of...
How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of...
How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of...
How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of...
How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of...
Secondary Loops
Secondary Loops       new WP_Query()$my_query = new WP_Query();while( $my_query->have_posts() ):    $my_query->the_post();...
Secondary Loops            get_posts()get_posts() uses the WP_Query class tofetch an array of post objects$post->post_cont...
Secondary Loops                        get_posts()$post = get_posts( array(    ‘posts_per_page’ => 7! ! ‘cat’ => -3,));!  ...
Secondary Loops                    get_posts()$post = get_posts( array(    ‘posts_per_page’ => 7! ! ‘cat’ => -3,));/** * *...
Secondary Loops                    get_posts()$post = get_posts( array(    ‘posts_per_page’ => 7! ! ‘cat’ => -3,));foreach...
Secondary Loops                    get_posts()$post = get_posts( array(    ‘posts_per_page’ => 7! ! ‘cat’ => -3,));**/ * *...
Secondary Loops                    get_posts()$post = get_posts( array(   ‘posts_per_page’ => 7! ! ‘cat’ => -3,));foreach(...
query_posts()
query_posts()query_posts() sets up The Loop withquery parameters. It also overrides thecurrent WordPress Loop.
query_posts()query_posts( array(   ‘posts_per_page’ => 7! ! ‘cat’ => -3,));if ( have_posts() ) : while ( have_posts() : th...
query_posts()query_posts( array(   ‘posts_per_page’ => 7! ! ‘cat’ => -3,));**/ * If you use query_posts() you HAVE to use ...
query_posts()query_posts( array(   ‘posts_per_page’ => 7! ! ‘cat’ => -3,));if ( have_posts() ) : while ( have_posts() : th...
query_posts()      query_posts() alters destroys the         global $wp_query variables/** * * This will cause WordPress t...
When should we use  query_posts()?
query_posts()The Problem with query_posts() is that itis always a secondary loop that tries tobe the main one and fails mi...
When should we use  query_posts()?
When should we use  query_posts()?NEVER USE query_posts()
When should we use  query_posts()?NEVER USE query_posts()     query_posts()
What if I want to alter       the query?
pre_get_posts()
pre_get_posts()pre_get_posts fires every time WordPressfetches posts from the database.nav menus, new wp_query, query_post...
What if I only want toalter the main query?
is_main_query()
is_main_query()function alter_query( $query ) {! if ( $query->is_main_query() && $query->is_home() )! !     $query->set( ‘...
What happens when we    run a query?
What happens when we     run a query?WordPress actually runs 4 SQL queries      every time a query is run
$my_query = new WP_Query(); while( $my_query->have_posts() ):   $my_query->the_post();
$my_query = new WP_Query();  while( $my_query->have_posts() ):     $my_query->the_post();1)Gets the posts:  SELECT SQL_CAL...
$my_query = new WP_Query();  while( $my_query->have_posts() ):     $my_query->the_post();1)Gets the posts:  SELECT SQL_CAL...
$my_query = new WP_Query();  while( $my_query->have_posts() ):     $my_query->the_post();1)Gets the posts:  SELECT SQL_CAL...
$my_query = new WP_Query();  while( $my_query->have_posts() ):     $my_query->the_post();1)Gets the posts:  SELECT SQL_CAL...
What if I don’t needpagination, meta, or       terms?
What if I don’t needpagination, meta, or       terms?You can selectively turn these off
What if I don’t needpagination, meta, or       terms?  You can selectively turn these off$my_query = new WP_Query( array( ...
<review>$wp_query is always the main queryif you use query_posts(), $wp_query now holds the queryyou just created.
<review>$wp_query is always the main queryif you use query_posts(), $wp_query now holds the queryyou just created.Use pre_...
<review>$wp_query is always the main queryif you use query_posts(), $wp_query now holds the queryyou just created.Use pre_...
<review>$wp_query is always the main queryif you use query_posts(), $wp_query now holds the queryyou just created.Use pre_...
</review>
questions?
Upcoming SlideShare
Loading in …5
×

The Query the Whole Query and Nothing but the Query

2,056 views

Published on

Published in: Technology
  • Be the first to comment

The Query the Whole Query and Nothing but the Query

  1. 1. WordCamp Austin 2012
  2. 2. Chris OlbeksonWordPress ConsultantC3M Digital@Chris_Olbekson on Twitterchris@c3mdigital.comWordCamp Austin 2012
  3. 3. The Query, The WholeQuery, & Nothing But $the_query
  4. 4. What is the query?
  5. 5. What is the query? The Loop
  6. 6. What is the query? The Loop while( have_posts() ): the_post(); the_content(); endwhile;
  7. 7. What is the query? WP_Query class
  8. 8. What is the query? $wp_query object
  9. 9. class WP_Query
  10. 10. $wp_query objectThe WP Class gives the $wp_query objectinformation that defines the currentrequest.
  11. 11. class WP_Query $wp_query objectThe WP Class gives the $wp_query objectinformation that defines the currentrequest.$wp_query determines the query based onthat information and fetches therequested posts
  12. 12. class WP_Queryclass WP { function main() { $this->init(); $this->parse_request(); $this->query_posts(); $this->handle_404(); $this->register_globals() $wp_query object
  13. 13. class WP_Query Properties$query, $query_vars, $posts, $post_count,$current_post, $queried_object_id, etc... Booleans: is_single(), is_category(), is_paged(),etc...
  14. 14. class WP_Query Methodsinit(), parse_query(), set( $query_var, $value ),have_posts(), rewind_posts() etc...
  15. 15. class WP_QueryInteracting with WP_Query
  16. 16. class WP_Query Interacting with WP_QueryBefore the loopAltering the query, filters
  17. 17. class WP_Query Interacting with WP_QueryDuring the loopConditionals and template tags,secondary queries, get_sidebar(), moresecondary queries, get_footer().
  18. 18. How do we Query?
  19. 19. How do we Query?query_posts() alters the main query
  20. 20. How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries
  21. 21. How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of post objects
  22. 22. How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of post objectsget_posts() use for secondary queries
  23. 23. How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of post objectsget_posts() use for secondary queries get_posts() - wrapper for a separate instance of WP_Query returns an array of post objects
  24. 24. How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of post objectsget_posts() use for secondary queries get_posts() - wrapper for a separate instance of WP_Query returns an array of post objects$wpdb->roll your own
  25. 25. How do we Query?query_posts() alters the main querynew WP_Query() use for secondary queries WP_Query - returns an array of post objectsget_posts() use for secondary queries get_posts() - wrapper for a separate instance of WP_Query returns an array of post objects$wpdb->roll your own $wpdb uses the wpdb class to execute any SQL query on the WordPress database
  26. 26. Secondary Loops
  27. 27. Secondary Loops new WP_Query()$my_query = new WP_Query();while( $my_query->have_posts() ): $my_query->the_post(); // do stuff endwhile;
  28. 28. Secondary Loops get_posts()get_posts() uses the WP_Query class tofetch an array of post objects$post->post_content, $post->post_titleetc...
  29. 29. Secondary Loops get_posts()$post = get_posts( array( ‘posts_per_page’ => 7! ! ‘cat’ => -3,));! foreach( $posts as $post ) { echo $post->post_title; echo apply_filters( ‘the_content’, $post->post_content ); }
  30. 30. Secondary Loops get_posts()$post = get_posts( array( ‘posts_per_page’ => 7! ! ‘cat’ => -3,));/** * * To make post template tags ie: the_content() * available when using get_posts() call the * internal function setup_postdata() with the * the $post array as its argument * */
  31. 31. Secondary Loops get_posts()$post = get_posts( array( ‘posts_per_page’ => 7! ! ‘cat’ => -3,));foreach( $posts as $post ) {! setup_postdata( $post ); the_title(); the_content(); }
  32. 32. Secondary Loops get_posts()$post = get_posts( array( ‘posts_per_page’ => 7! ! ‘cat’ => -3,));**/ * * After looping through a separate query, * wp_reset_postdata() restores the $post global * to the current post in the main query * */
  33. 33. Secondary Loops get_posts()$post = get_posts( array( ‘posts_per_page’ => 7! ! ‘cat’ => -3,));foreach( $posts as $post ) {! setup_postdata( $post ); the_title(); the_content(); } wp_reset_postdata();
  34. 34. query_posts()
  35. 35. query_posts()query_posts() sets up The Loop withquery parameters. It also overrides thecurrent WordPress Loop.
  36. 36. query_posts()query_posts( array( ‘posts_per_page’ => 7! ! ‘cat’ => -3,));if ( have_posts() ) : while ( have_posts() : the_post(); the_title(); the_content();endwhile; endif;
  37. 37. query_posts()query_posts( array( ‘posts_per_page’ => 7! ! ‘cat’ => -3,));**/ * If you use query_posts() you HAVE to use * wp_reset_query() to restore the $post globals * */
  38. 38. query_posts()query_posts( array( ‘posts_per_page’ => 7! ! ‘cat’ => -3,));if ( have_posts() ) : while ( have_posts() : the_post(); the_title(); the_content();endwhile; endif; wp_reset_query();
  39. 39. query_posts() query_posts() alters destroys the global $wp_query variables/** * * This will cause WordPress to run the query it * thought you wanted and the one you’re actually going * to use. * * breaks widgets * pagination breaks * conditional tags will not work * and more... * */
  40. 40. When should we use query_posts()?
  41. 41. query_posts()The Problem with query_posts() is that itis always a secondary loop that tries tobe the main one and fails miserably.
  42. 42. When should we use query_posts()?
  43. 43. When should we use query_posts()?NEVER USE query_posts()
  44. 44. When should we use query_posts()?NEVER USE query_posts() query_posts()
  45. 45. What if I want to alter the query?
  46. 46. pre_get_posts()
  47. 47. pre_get_posts()pre_get_posts fires every time WordPressfetches posts from the database.nav menus, new wp_query, query_posts,get_posts, that slider plugin your clientinstalled, everything.
  48. 48. What if I only want toalter the main query?
  49. 49. is_main_query()
  50. 50. is_main_query()function alter_query( $query ) {! if ( $query->is_main_query() && $query->is_home() )! ! $query->set( ‘cat’, ‘-3’ );}add_action( ‘pre_get_posts’, ‘alter_query’ );
  51. 51. What happens when we run a query?
  52. 52. What happens when we run a query?WordPress actually runs 4 SQL queries every time a query is run
  53. 53. $my_query = new WP_Query(); while( $my_query->have_posts() ): $my_query->the_post();
  54. 54. $my_query = new WP_Query(); while( $my_query->have_posts() ): $my_query->the_post();1)Gets the posts: SELECT SQL_CALC_FOUND_ROWS... FROM wp_post LIMIT 0, 10
  55. 55. $my_query = new WP_Query(); while( $my_query->have_posts() ): $my_query->the_post();1)Gets the posts: SELECT SQL_CALC_FOUND_ROWS... FROM wp_post LIMIT 0, 102)Counts the posts for pagination: SELECT FOUND_ROWS()
  56. 56. $my_query = new WP_Query(); while( $my_query->have_posts() ): $my_query->the_post();1)Gets the posts: SELECT SQL_CALC_FOUND_ROWS... FROM wp_post LIMIT 0, 102)Counts the posts for pagination: SELECT FOUND_ROWS()3)Gets all the metadata: SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN...
  57. 57. $my_query = new WP_Query(); while( $my_query->have_posts() ): $my_query->the_post();1)Gets the posts: SELECT SQL_CALC_FOUND_ROWS... FROM wp_post LIMIT 0, 102)Counts the posts for pagination: SELECT FOUND_ROWS()3)Gets all the metadata: SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN...4)Gets all the terms SELECT… AS… INNER JOIN wp_term_taxonomy AS... INNER JOIN wp_term_relationships AS... ON... IN...
  58. 58. What if I don’t needpagination, meta, or terms?
  59. 59. What if I don’t needpagination, meta, or terms?You can selectively turn these off
  60. 60. What if I don’t needpagination, meta, or terms? You can selectively turn these off$my_query = new WP_Query( array( ‘no_found_rows’ = true, ‘update_post_meta_cache’ => false, ‘update_post_term_cache’ => false, ) );
  61. 61. <review>$wp_query is always the main queryif you use query_posts(), $wp_query now holds the queryyou just created.
  62. 62. <review>$wp_query is always the main queryif you use query_posts(), $wp_query now holds the queryyou just created.Use pre_get_posts() when you need to alter the main query.It’s cleaner the “right” way to do it and your clients andfellow developers will love you.
  63. 63. <review>$wp_query is always the main queryif you use query_posts(), $wp_query now holds the queryyou just created.Use pre_get_posts() when you need to alter the main query.It’s cleaner the “right” way to do it and your clients andfellow developers will love you.For secondary queries - custom sliders, sidebars, etc...use get_posts() or create a new WP_Query
  64. 64. <review>$wp_query is always the main queryif you use query_posts(), $wp_query now holds the queryyou just created.Use pre_get_posts() when you need to alter the main query.It’s cleaner the “right” way to do it and your clients andfellow developers will love you.For secondary queries - custom sliders, sidebars, etc...use get_posts() or create a new WP_QueryIf you call the_post(), or set_up_postdata() usewp_reset_postdata(). If you have to use query_posts() usewp_reset_query().
  65. 65. </review>
  66. 66. questions?

×