<ul>What is the WP_Query Class? </ul><ul><li>Its the query builder for pulling post objects from the database
Returned posts are based on passed in arguments </li></ul>
Important Properties of WP_Query <ul><li>query_vars </li><ul><li>An associative array describing the query
The values in this array are used within the WP_Query->get_posts() method to build the final SQL query </li></ul></ul>
Important Properties of WP_Query <ul><li>Template Properties </li><ul><li>Used mainly in template handling such as templat...
Also used within the WP_Query->get_posts() method to assign defaults for required/unset query_vars </li></ul></ul>
Important Properties of WP_Query <ul><li>Template Properties (cont) </li><ul><li>Examples: </li><ul><li>is_home – if query...
is_day – if query contains a day
is_feed – if query is for feed display
is_page - if query return single page (post type page)
is_single – if the query is for a single post
Upcoming SlideShare
Loading in...5
×

WP_Query Overview

4,347

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,347
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

WP_Query Overview

  1. 1. <ul>What is the WP_Query Class? </ul><ul><li>Its the query builder for pulling post objects from the database
  2. 2. Returned posts are based on passed in arguments </li></ul>
  3. 3. Important Properties of WP_Query <ul><li>query_vars </li><ul><li>An associative array describing the query
  4. 4. The values in this array are used within the WP_Query->get_posts() method to build the final SQL query </li></ul></ul>
  5. 5. Important Properties of WP_Query <ul><li>Template Properties </li><ul><li>Used mainly in template handling such as template-loader.php to decide which template to load
  6. 6. Also used within the WP_Query->get_posts() method to assign defaults for required/unset query_vars </li></ul></ul>
  7. 7. Important Properties of WP_Query <ul><li>Template Properties (cont) </li><ul><li>Examples: </li><ul><li>is_home – if query is blog homepae
  8. 8. is_day – if query contains a day
  9. 9. is_feed – if query is for feed display
  10. 10. is_page - if query return single page (post type page)
  11. 11. is_single – if the query is for a single post
  12. 12. is_singular – if the query is for a single post object
  13. 13. ... </li></ul></ul></ul>
  14. 14. Important Properties of WP_Query <ul><li>Template Properties (cont) </li><ul><li>Each also has matching function to return the value from the global $wp_query instance, see http://codex.wordpress.org/Conditional_Tags </li></ul></ul>
  15. 15. Important Properties of WP_Query <ul><li>Post result properties </li><ul><li>Examples: </li><ul><li>found_posts – number of posts that would have been returned if not paging
  16. 16. max_num_pages – number of pages based on the current posts_per_page and found_posts
  17. 17. post_count – number of posts in the current posts array
  18. 18. posts – array containing posts result </li></ul></ul></ul>
  19. 19. How to Use the WP_Query Class <ul><li>new WP_Query() </li><ul><li>Use when you need to loop through posts, but don't want to replace the global $wp_query instance </li><ul><li>Must use the instance to loop through posts </li><ul><li>$my_wp_query->have_posts()
  20. 20. $my_wp_query->the_post() </li></ul></ul><li>Example: </li><ul><li>$lead_query = new WP_Query(array('post_type'=>'lead', 'lead-position'=>'Homepage', 'posts_per_page'=>5)); </li></ul></ul></ul>
  21. 21. How to Use the WP_Query Class <ul><li>query_posts() </li><ul><li>Use when you need to replace the global $wp_query instance </li><ul><li>other parts within the theme like the header, sidebar, footer need to reflect the new query
  22. 22. some template functions only use the global instance, ie next_post_link(), prev_post_link(), etc.
  23. 23. Example: query_posts(array('meta_key'=>'show', 'meta_value'=> $show->ID, 'paged'=>get_query_var('paged'))); </li></ul></ul></ul>
  24. 24. How to Use the WP_Query Class <ul><li>get_posts() </li><ul><li>Use when you just need to return a array of the raw post data (no filters applied) </li><ul><li>Will not be able to use any of the template functions unless setup_postdata($post) is called
  25. 25. Be carefule of using reserved global variable names like $post, $page, $paged, etc as it will cause conflicts </li></ul></ul><li>Example: get_posts( array('post_type' => 'background', 'category_name' => $cur_page->slug, 'numberposts' => 1, 'suppress_filters' => false)); </li></ul>
  26. 26. Modifying the Query Using Filters <ul><li>Available Filters </li><ul><li>posts_where – allows modification of the where clause
  27. 27. posts_join – allows modification of the join statement
  28. 28. posts_orderby – allows modification of the order by statement </li></ul></ul>
  29. 29. Modifying the Query Using Filters <ul><li>Each filter is passed a reference to the calling WP_Query instance allowing query_vars and other properties to be checked before altering that part of the query
  30. 30. Example: </li></ul>function filter_where($where, $wp_query) { if(!is_admin() && ($wp_query->get('post_type') == 'background')) { $cur_time = time(); $where.= &quot; AND (EndDate.meta_value is null OR EndDate.meta_value >= '$cur_time'; } return $where; }
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×