Everything You Need to Know About WP_Query, WordCamp Russia 2014

  • View
    669

  • Download
    3

Embed Size (px)

DESCRIPTION

http://2014.russia.wordcamp.org/

Text of Everything You Need to Know About WP_Query, WordCamp Russia 2014

  • 1. , WP_Query WordCamp Russia 2014

2. WordPress http://core.trac.wordpress.org WP http://ru.wordpress.org http://sergeybiryukov.ru @flash_usb 3. WP_Query? 4. is_author(), is_category(), is_home() .. 5. query_posts()? 6. query_posts() new WP_Query() get_posts() 7. WordPress while ( have_posts() ) : the_post(); endwhile; 8. $query = new WP_Query( ); while ( $query->have_posts() ) : $query->the_post(); endwhile; 9. $result = get_posts( ); foreach ( $result as $post_obj ) { ... } 10. ? 11. is_author() , $wp_query->is_author(). 12. function is_author() { global $wp_query; return $wp_query->is_author(); } 13. while ( have_posts() ) : the_post(); if ( is_author() ) echo ' '; endwhile; 14. while ( have_posts() ) : the_post(); if ( $wp_query->is_author() ) echo ' '; endwhile; 15. $query = new WP_Query( ); while ( $query->have_posts() ) : $query->the_post(); if ( $query->is_author() ) echo ' .'; endwhile; 16. $query = new WP_Query( ); while ( $query->have_posts() ) : $query->the_post(); if ( $query->is_author() ) echo ' .'; endwhile; 17. $query = new WP_Query( ); while ( $query->have_posts() ) : $query->the_post(); if ( $query->is_author() ) echo ' .'; endwhile; 18. : $my_query = new WP_Query( $query ); : while ( $my_query->have_posts() ) : $my_query->the_post(); endwhile; wp_reset_postdata(); 19. wp_reset_postdata() wp_reset_query()? query_posts()? ? ? 20. , ? 21. wp-blog-header.php // WordPress require './wp-load.php'; // , require WPINC . '/template-loader.php'; 22. ? $wp_the_query = new WP_Query(); $wp_query =& $wp_the_query; 23. PHP $a = 4; $b =& $a; $b = 2; var_dump( $a ); // int(2) $a = 6; var_dump( $b ); // int(6) 24. $wp_the_query. $wp_query. 25. wp-blog-header.php // WordPress require './wp-load.php'; // , require WPINC . '/template-loader.php'; 26. wp-blog-header.php // WordPress require './wp-load.php'; // wp(); // , require WPINC . '/template-loader.php'; 27. wp()? function wp( $query_vars = '' ) { global $wp; $wp->main( $query_vars ); } 28. ?! 29. $wp = new WP(); wp() WP. 30. class WP { function main() { $this->init(); $this->parse_request(); $this->send_headers(); $this->query_posts(); $this->handle_404(); $this->register_globals(); ... 31. class WP { function main() { $this->init(); $this->parse_request(); $this->send_headers(); $this->query_posts(); $this->handle_404(); $this->register_globals(); ... 32. WP::parse_request() URL WP_Rewrite WP_Query WP::query_posts() { global $wp_the_query; $wp_the_query->query( $this->query_vars ); } 33. SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish' ORDER BY wp_posts.post_date DESC LIMIT 0, 10 34. wp-blog-header.php // WordPress require './wp-load.php'; // , , wp(); // require WPINC . '/template-loader.php'; 35. , . 36. ? query_posts( 'author=-5' ); get_header(); while ( have_posts() ) : the_post(); endwhile; get_footer(); 37. ! , WordPress . , . 38. * WP_Query , . 39. 1. : SELECT SQL_CALC_FOUND_ROWS FROM wp_posts LIMIT 0, 10 2. ? SELECT FOUND_ROWS() 3. . 4. . 40. ( ...) $my_query = new WP_Query( array( 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); 41. . 42. query_posts() 43. WordPress , . 44. query_posts( array( 'author' => -5, 'posts_per_page' => 25, ) ); . 45. , .. 46. query_posts() ? 47. pre_get_posts class WP_Query { function get_posts() { $this->parse_query(); // ! do_action_ref_array( 'pre_get_posts', array( &$this ) ); 48. function alter_my_home_query( $query ) { if ( $query->is_home() ) $query->set( 'author', '-5' ); } add_action( 'pre_get_posts', 'alter_my_home_query' ); 49. . 50. 'pre_get_posts' get_posts() new WP_Query , . .. 51. ? 52. $wp_the_query 53. WP_Query::is_main_query() class WP_Query { function is_main_query() { global $wp_the_query; return $wp_the_query === $this; } 54. ! function alter_my_home_query( $query ) { if ( $query->is_home() && $query->is_main_query() ) $query->set( 'author', '-5' ); } add_action( 'pre_get_posts', 'alter_my_home_query' ); 55. function alter_my_home_query( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_home() ) $query->set( 'author', '-5' ); } add_action( 'pre_get_posts', 'alter_my_home_query' ); 56. WP_Query::is_main_query()? $wp_the_query . $wp_query $wp_the_query, query_posts(). is_main_query(). 57. query_posts( 'author=-5' ); while ( have_posts() ) : the_post(); endwhile; wp_reset_query(); 58. query_posts( 'author=-5' ); while ( have_posts() ) : the_post(); endwhile; wp_reset_query(); 59. function query_posts( $query ) { // $wp_the_query unset( $wp_query ); $wp_query =& new WP_Query( $query ); return $wp_query; } 60. query_posts( 'author=-5' ); while ( have_posts() ) : the_post(); endwhile; wp_reset_query(); 61. function wp_reset_query() { // $wp_the_query unset( $wp_query ); $wp_query =& $wp_the_query; // wp_reset_postdata(); } 62. the_post()? wp_reset_query() $wp_query . $my_query->the_post()? wp_reset_postdata() . 63. ? 64. /* Template: My Template */ query_posts( $query_string . '&author=-5&posts_per_page=25' ); get_header(); while ( have_posts() ) : the_post(); endwhile; 65. function alter_my_template( $query ) { if ( ! $query->is_main_query() ) return; if ( ! is_page_template( 'my-template.php' ) ) return; $query->set( 'author', '-5' ); $query->set( 'posts_per_page', 25 ); } add_action( 'pre_get_posts', 'alter_my_template' ); 66. WP_Query , . $wp_query . $wp_query , query_posts(). wp_reset_query(). 67. 'pre_get_posts' ( ). $query->is_main_query(), . 68. http://sergeybiryukov.ru @flash_usb ! ?