The Query the Whole Query and Nothing but the Query

1,943 views
1,875 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,943
On SlideShare
0
From Embeds
0
Number of Embeds
139
Actions
Shares
0
Downloads
35
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. WP class in gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • \n
  • WP::parse_request()\n- Parses the URL using WP_Rewrite\n- Sets up query variables for WP_Query\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 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?

    ×