The Query the Whole Query and Nothing but the Query
Upcoming SlideShare
Loading in...5
×
 

The Query the Whole Query and Nothing but the Query

on

  • 1,880 views

 

Statistics

Views

Total Views
1,880
Views on SlideShare
1,747
Embed Views
133

Actions

Likes
2
Downloads
33
Comments
0

3 Embeds 133

http://c3mdigital.com 90
http://wp.beta 42
http://zdrive 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 The Query the Whole Query and Nothing but the Query Presentation Transcript

  • 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 currentrequest.$wp_query determines the query based onthat information and fetches therequested posts
  • class WP_Queryclass WP { function main() { $this->init(); $this->parse_request(); $this->query_posts(); $this->handle_404(); $this->register_globals() $wp_query object
  • class WP_Query Properties$query, $query_vars, $posts, $post_count,$current_post, $queried_object_id, etc... Booleans: is_single(), is_category(), is_paged(),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(), moresecondary queries, get_footer().
  • 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 post objects
  • 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
  • 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
  • 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
  • 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
  • Secondary Loops
  • Secondary Loops new WP_Query()$my_query = new WP_Query();while( $my_query->have_posts() ): $my_query->the_post(); // do stuff endwhile;
  • Secondary Loops get_posts()get_posts() uses the WP_Query class tofetch an array of post objects$post->post_content, $post->post_titleetc...
  • 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 ); }
  • 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 * */
  • 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(); }
  • 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 * */
  • 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();
  • 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() : the_post(); the_title(); the_content();endwhile; endif;
  • 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 * */
  • 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();
  • 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... * */
  • 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 miserably.
  • 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_posts,get_posts, that slider plugin your clientinstalled, everything.
  • 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( ‘cat’, ‘-3’ );}add_action( ‘pre_get_posts’, ‘alter_query’ );
  • 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_CALC_FOUND_ROWS... FROM wp_post LIMIT 0, 10
  • $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()
  • $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...
  • $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...
  • 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( ‘no_found_rows’ = true, ‘update_post_meta_cache’ => false, ‘update_post_term_cache’ => false, ) );
  • <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_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.
  • <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
  • <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().
  • </review>
  • questions?