WP_Query, pre_get_posts, and eliminating query_posts()

11,305 views
11,071 views

Published on

At the heart of WordPress is the WP_Query class, used to retrieve practically all content from the database and prepare it for display. In this presentation, I’ll discuss the myriad arguments available with WP_Query, the various properties and methods that are automatically populated, and the advanced queries made possible through filters within the class. I’ll also review the pre_get_posts action, detail how it can be used to modify standard WordPress queries and completely eliminate query_posts(), and explain why you’d want to do so in the first place.

Published in: Technology
2 Comments
7 Likes
Statistics
Notes
  • Is OOmph in Boston?
    Page 7 got me here. Anywhere else I can get more insight on the rules of using string params instead of arrays?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi Erick Hitter,
    Please Help me to solve my problem.I want to sort post with featured image post first and then rest of post using wp_query.


    Thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
11,305
On SlideShare
0
From Embeds
0
Number of Embeds
1,169
Actions
Shares
0
Downloads
85
Comments
2
Likes
7
Embeds 0
No embeds

No notes for slide

WP_Query, pre_get_posts, and eliminating query_posts()

  1. 1. FUN WITH WP_QUERY Erick Hitter @ethitterLead WordPress Developer at Oomph, Inc. WordPress 3.3 Core Contributor WordCamp Boston Organizer Plugin Author
  2. 2. WHAT IS WP_QUERY?WordPress class defined in wp-includes/query.php.Responsible for majority of post-type retrieval from database.Any front-end request made to WordPress is fulfilled byWP_Query in what I refer to as the main query.Used extensively by WordPress but also available to developers.
  3. 3. USING WP_QUERYMost often, this is done in "The Loop" to use the results from themain query.<?php if ( have_posts() ) { while( have_posts() ) { the_post(); } }?>
  4. 4. USING WP_QUERY: NEW WP_QUERY();<?php $queried_posts = new WP_Query(); if ( $queried_posts->have_posts() ) { while( $queried_posts->have_posts() ) { $queried_posts->the_post(); //Use functions such as the_title() and the_content() here. } } wp_reset_query(); wp_reset_postdata();?> Creates a new object of the type WP_Query, setting up another instance of The Loop. wp_reset_query() and wp_reset_postdata() are necessary to restore the main WordPress query. More on this later. Best used whenever Template Tags are needed.
  5. 5. USING WP_QUERY: GET_POSTS();<?php $queried_posts = get_posts(); foreach( $queried_posts as $queried_post ) { setup_postdata( $queried_post ); } wp_reset_postdata();?> get_posts() generates an array of post objects that match the query parameters. setup_postdata() and wp_reset_postdata() can be used if Template Tags, such as the_title(), are needed. Best used when Template Tags arent needed or post data will be repurposed, such as generating an array of post IDs.
  6. 6. RESETTING QUERY ANDPOSTDATAAfter using new WP_Query(), calling wp_reset_query() and wp_reset_postdata() restoresthe original query and postdata.After using setup_postdata(), calling wp_reset_postdata() restores the post.Calling the two reset functions ensures that the Conditional Tags operate as expected and balance ofpage renders properly.
  7. 7. QUERY BASICSCan pass parameters as an array or query string.<?php $foo = new WP_Query( array( posts_per_page => 5 ) ); $bar = new WP_Query( posts_per_page=5 );?>Array format is more flexible and necessary for queries such as: post__not_in tax_query meta_queryQuery string format is parsed into an array by the WP_Query class.
  8. 8. QUERY PARAMETERSExtensive list in the Codex at http://codex.wordpress.org/Class_Reference/WP_Query#ParametersKnowing default parameters for query reduces duplication. post_type => post orderby => date order => DESC posts_per_page => Value set under Settings -> ReadingCan query for (among many others): Author Date/time Post type Taxonomy term assignments Meta data
  9. 9. SAMPLE QUERY 1Five most recent posts<?php $foo = new WP_Query( array( posts_per_page => 5) ); ?>
  10. 10. SAMPLE QUERY 2Five most recent posts by author with ID 15<?php $foo = new WP_Query( array( posts_per_page => 5, author => 15) ); ?>
  11. 11. SAMPLE QUERY 3Five most recent posts by author with ID 15 in the toast<?php $foo = new WP_Query( array( posts_per_page => 5, author => 15, category_name => toast) ); ?>
  12. 12. SAMPLE QUERY 4Five most recent posts or pages<?php $foo = new WP_Query( array( posts_per_page => 5, post_type => array( post, page )) ); ?>
  13. 13. USING THE TAX_QUERYIntroduced in WordPress 3.1Permits multiple taxonomy queries within a single WP_Query.Important to remember that the tax_query parameter takes an array of arrays, even if queryingfor a single taxonomy term.Each query accepts up to five arguments: taxonomy (required) - string field - term_id or slug terms (required) - string or array include_children - boolean, defaults to true operator - string, either AND, IN, or NOT IN
  14. 14. USING THE TAX_QUERYUsing the toast example from Sample Query 3:<?php $foo = new WP_Query( array( posts_per_page => 5, author => 15, category_name => toast) ); ?>Becomes<?php $foo = new WP_Query( array( posts_per_page => 5, author => 15, tax_query => array( array( taxonomy => category, field => slug, terms => toast ) )) ); ?>
  15. 15. USING THE TAX_QUERY: MULTIPLE QUERIESQuerying two categories:<?php $foo = new WP_Query( array( posts_per_page => 5, author => 15, tax_query => array( array( taxonomy => category, field => slug, terms => array( toast, breakfast ), operator => AND ) )) ); ?>Querying a category and a tag:<?php $foo = new WP_Query( array( posts_per_page => 5, author => 15, tax_query => array( relation => AND, array( taxonomy => category, field => slug, terms => toast ), array( taxonomy => post_tag,
  16. 16. field => slug, terms => wheat ) )) ); ?>
  17. 17. USING THE TAX_QUERY: MULTIPLE QUERIESQuerying a category and a tag, excluding subcategories:<?php $foo = new WP_Query( array( posts_per_page => 5, author => 15, tax_query => array( relation => AND, array( taxonomy => category, field => slug, terms => toast, include_children => false ), array( taxonomy => post_tag, field => slug, terms => wheat ) )) ); ?>
  18. 18. USING THE META_QUERYIntroduced in WordPress 3.1, along with tax_queryPermits multiple post meta queries within a single WP_Query.Like the tax_query, it is important to note that the meta_query parameter takes an array ofarrays.Each query accepts up to four arguments: key (required) - string value - string or array compare - string, such as =, !=, or IN. Full list on the WP_Query Codex page. type - string, such as NUMERIC or DATE. Full list on the WP_Query Codex page.
  19. 19. USING THE META_QUERYFive posts with Featured Images<?php $foo = new WP_Query( array( posts_per_page => 5, meta_query => array( array( key => _thumbnail_id ) )) ); ?>Five posts using the same thumbnail, ID 100<?php $foo = new WP_Query( array( posts_per_page => 5, meta_query => array( array( key => _thumbnail_id, value => 100, type => NUMERIC ) )) ); ?>
  20. 20. USING THE META_QUERY: MULTIPLE QUERIESFive posts with Featured Images AND flagged as "featured" posts<?php $foo = new WP_Query( array( posts_per_page => 5, meta_query => array( array( key => _thumbnail_id ), array( key => _featured ) )) ); ?>Five posts with Featured Images OR flagged as "featured" posts<?php $foo = new WP_Query( array( posts_per_page => 5, meta_query => array( relation => OR, array( key => _thumbnail_id ), array( key => _featured ) )) ); ?>
  21. 21. GET_QUERY_VAR();Used to retrieve a query parameter from the main query.<?php $qty = get_query_var( posts_per_page ); $post_type = get_query_var( post_type );?>
  22. 22. IS_MAIN_QUERY();Introduced in WordPress 3.3Simple conditional tag that determines whether or not the current query is the main WordPress query.
  23. 23. QUERY_POSTS(); Evil WordPress function provided to modify or override main query. Accepts query arguments as described in the preceeding slides. Used within a theme file (archive.php, home.php, index.php, single.php, etc). When called, original main query is replaced by the query specified by query_posts(). Introduces unnecessary performance degredation.Thankfully, a far-superior alternative exists!
  24. 24. THE PRE_GET_POSTS ACTIONAction is executed right before WP_Query parses the query arguments.Allows any and all query arguments to be modified before a database query is ever run.Can be used to universally modify queries on a WordPress site, or to modify specific queries.Rather than placing modifications in individual theme files as is done with query_posts(), changescan be centralized in functions.php.
  25. 25. USING THE PRE_GET_POSTS ACTION<?php function bwpm_pre_get_posts( $query ) { //Herein, modify the query } add_action( pre_get_posts, bwpm_pre_get_posts );?>The $query variable contains an object with two helpful methods: get( $query_parameter ) - retrieves a specified query parameter from the current query. set( $query_parameter, $value ) - sets a specified query parameter in the current query.
  26. 26. USING THE PRE_GET_POSTS ACTIONShow only five posts on any and every archive page:<?php function bwpm_pre_get_posts( $query ) { $query->set( posts_per_page, 5 ); } add_action( pre_get_posts, bwpm_pre_get_posts );?>To do the same on just the front page:<?php function bwpm_pre_get_posts( $query ) { if ( $query->is_home() ) $query->set( posts_per_page, 5 ); } add_action( pre_get_posts, bwpm_pre_get_posts );?>
  27. 27. USING THE PRE_GET_POSTS ACTIONTo do the same only when a posts_per_page value isnt set (is using theWordPress default):<?php function bwpm_pre_get_posts( $query ) { if ( ! $query->get( posts_page_page ) ) $query->set( posts_per_page, 5 ); } add_action( pre_get_posts, bwpm_pre_get_posts );?>
  28. 28. QUESTIONS?

×