Slide 1

Slide 1 text

Advanced WP_Query Alison Barrett · @alisothegeek

Slide 2

Slide 2 text

Alison Barrett Senior Web Engineer at 10up @alisothegeek

Slide 3

Slide 3 text

Custom Loops

Slide 4

Slide 4 text

query_posts(  'cat=3'  );

Slide 5

Slide 5 text

$wp_query

Slide 6

Slide 6 text

$my_query  =  new  WP_Query(  'orderby=title&order=ASC'  );   ! $my_query  =  new  WP_Query(  array(     'orderby'  =>  'title',     'order'      =>  'ASC',   )  );

Slide 7

Slide 7 text

if  (  have_posts()  )  :       while  (  have_posts()  )  :           the_post();

Slide 8

Slide 8 text

if  (  $my_query-­‐>have_posts()  )  :       while  (  $my_query-­‐>have_posts()  )  :           $my_query-­‐>the_post();

Slide 9

Slide 9 text

    endwhile;   endif;   wp_reset_postdata(); Resets the $post global for the main query

Slide 10

Slide 10 text

WP_Query Parameters

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

$my_query  =  new  WP_Query(  array(     'cat'  =>  3,   )  );

Slide 14

Slide 14 text

$my_query  =  new  WP_Query(  array(     'posts_per_page'            =>  5,     'orderby'                          =>  'comment_count',     'ignore_sticky_posts'  =>  true,   )  );

Slide 15

Slide 15 text

$my_query  =  new  WP_Query(  array(     'post_type'  =>  'ag_movie_review',     'tax_query'  =>  array(       'relation'  =>  'AND',       array(         'taxonomy'  =>  'ag_genre',         'field'        =>  'slug',         'terms'        =>  'thriller',       ),       array(         'taxonomy'  =>  'ag_actor',         'field'        =>  'slug',         'terms'        =>  'anthony-­‐hopkins',       ),       array(         'taxonomy'  =>  'ag_decade',         'field'        =>  'slug',         'terms'        =>  '2000s',       ),     ),   )  );

Slide 16

Slide 16 text

$my_query  =  new  WP_Query(  array(     'post_type'            =>  array(       'post',       'ag_debate',       'ag_interview'     ),     'meta_query'          =>  array(       array(         'key'          =>  '_ag_related_event',         'compare'  =>  'EXISTS',       ),     ),     'posts_per_page'  =>  12,   )  );

Slide 17

Slide 17 text

Altering the Main Query

Slide 18

Slide 18 text

query_posts(  'cat=3'  );

Slide 19

Slide 19 text

‣ muplugins_loaded ‣ … ‣ init ‣ parse_request ‣ parse_query ‣ pre_get_posts ‣ posts_selection ‣ … ‣ template_redirect Turn URL into query string Populate $wp_query object Pull posts from database Load template file

Slide 20

Slide 20 text

By the time the template loads, posts have already been chosen.

Slide 21

Slide 21 text

query_posts(  'cat=3'  ); This runs a second query.
 The old one is lost.

Slide 22

Slide 22 text

‣ muplugins_loaded ‣ … ‣ init ‣ parse_request ‣ parse_query ‣ pre_get_posts ‣ posts_selection ‣ … ‣ template_redirect Turn URL into query string Populate $wp_query object Alter $wp_query object Pull posts from database Load template file

Slide 23

Slide 23 text

function  ag_home_page_no_sticky(  $query  )  {     if  (  $query-­‐>is_home()  )  {       $query-­‐>set(  'ignore_sticky_posts',  true  );     }   }   add_action(  'pre_get_posts',  'ag_home_page_no_sticky'  );

Slide 24

Slide 24 text

function  ag_home_page_no_sticky(  $query  )  {     if  (  $query-­‐>is_home()  &&  $query-­‐>is_main_query()  )  {       $query-­‐>set(  'ignore_sticky_posts',  true  );     }   }   add_action(  'pre_get_posts',  'ag_home_page_no_sticky'  );

Slide 25

Slide 25 text

function  ag_movies_in_search(  $query  )  {     if  (  $query-­‐>is_search()  &&  $query-­‐>is_main_query()  )  {       $query-­‐>set(  'post_type',  array(  'post',  'ag_movie'  )  );     }   }   add_action(  'pre_get_posts',  'ag_movies_in_search'  );

Slide 26

Slide 26 text

Questions? @alisothegeek