wp query
TRANSCRIPT
![Page 1: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/1.jpg)
WordCamp Pune 2013
![Page 2: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/2.jpg)
Savita Soni, co-founder AmiWorks & chief-poet for WPoetsTwitter @savitas
![Page 3: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/3.jpg)
Power of WP_Query
![Page 4: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/4.jpg)
Ways to fetch posts & related data
• query_posts()• new WP_Query()• get_posts()• $wpdb global db object
![Page 5: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/5.jpg)
query_posts()
![Page 6: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/6.jpg)
How query_posts() looks
query_posts( $args );while ( have_posts() ) :
the_post();//the_title(), the_ID(), the_permalink() etc..
endwhile;
// Reset Query
wp_reset_query();
![Page 7: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/7.jpg)
new WP_Query()
![Page 8: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/8.jpg)
How WP_Query() looks
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) :$the_query->the_post();echo '<li>' . get_the_title() . '</li>';
endwhile;wp_reset_postdata();
![Page 9: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/9.jpg)
get_posts()
![Page 10: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/10.jpg)
How get_posts() looks
<?phpglobal $post;$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );$myposts = get_posts( $args );
foreach( $myposts as $post ) :setup_postdata($post); ?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
![Page 11: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/11.jpg)
This all we already know?
![Page 12: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/12.jpg)
So lets see something which we may not know...
![Page 13: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/13.jpg)
Conditional tags
is_author() is_home()is_archive()
we know this as well and use them frequently…
![Page 14: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/14.jpg)
But have you seen this?
$wp_query = new WP_Query( $args );$wp_query->is_author()$wp_query->is_home()$wp_query->is_archive()
Yes every query object has its own methods
![Page 15: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/15.jpg)
so we can do this as well
$my_query = new WP_Query( $args );while( $my_query->have_posts() ):
$my_query->the_post();if( $my_query->is_author()){ // do something for author
}endwhile;wp_reset_postdata();
![Page 16: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/16.jpg)
why we always needs to call
wp_reset_query()wp_reset_postdata();
with query_posts() or WP_Query()
![Page 17: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/17.jpg)
because when we do this
query_posts( 'cat=5');while ( have_posts() ) :
the_post();//the_title(), the_ID(), the_permalink() etc..
endwhile;
We have altered the main query...
![Page 18: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/18.jpg)
How?
global $wp_query; //this is the main query object
Do anyone know this?
$wp_the_query
So the real main query object $wp_the_query
and $wp_query is the live copy of $wp_the_query
![Page 19: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/19.jpg)
it is something like
$wp_the_query = new WP_Query();$wp_query = &$wp_the_query;
![Page 20: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/20.jpg)
when we call query_posts(), it works like this
function query_posts($query) {$GLOBALS['wp_query'] = new WP_Query();return $GLOBALS['wp_query']->query($query);
}
![Page 21: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/21.jpg)
and when we call wp_reset_query()
function wp_reset_query() {$GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; wp_reset_postdata();
}
![Page 22: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/22.jpg)
![Page 23: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/23.jpg)
how many query(s) get called when we call WP_Query()?
Is it running only one query with your passed arguments????
![Page 24: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/24.jpg)
it is running 4 queries
1. Select SQL_CALC_FOUND_ROWS....FROM wp_posts limit 0,10
2. Select FOUND_ROWS();
3. Get all metadata attached to these posts.
4. Get all terms for these posts.
![Page 25: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/25.jpg)
off some arguments selectively
new WP_Query(array(
'no_found_rows' => false,'update_post_meta_cache' => false,'update_post_term_cache' => false
))
![Page 26: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/26.jpg)
what if I need to modify the main query only?
Use pre_get_posts hook
![Page 27: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/27.jpg)
function exclude_category( $query ) { $query->set( 'cat', '-1,-1347' ); }add_action( 'pre_get_posts', 'exclude_category' );
![Page 28: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/28.jpg)
Now problem with this...
it gets called for every post queryget_posts()WP_Query()query_posts()
![Page 29: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/29.jpg)
what if I just need it for home page query?
now $wp_the_query object will help
function exclude_category( $query ) {global $wp_the_query;if( $wp_the_query == $query
and $query->home()){$query->set( 'cat', '-1,-1347' );
} }add_action( 'pre_get_posts', 'exclude_category' );
![Page 30: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/30.jpg)
After WordPress 3.3 we have an awesome function
is_main_query()
![Page 31: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/31.jpg)
So rather than writing
function exclude_category( $query ) {global $wp_the_query;//if( $wp_the_query == $query and $query->home()) if( $query->is_main_query() and $query->home()) {
$query->set( 'cat', '-1,-1347' );
}
![Page 32: Wp query](https://reader035.vdocuments.site/reader035/viewer/2022062419/557dbfb0d8b42a63048b461b/html5/thumbnails/32.jpg)
Conclusion
Try to avoid using query_posts() If query_posts() is used then reset it with
wp_reset_query() Use pre_get_posts hook to alter main query &
use it with is_main_query() condition tag