Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Секреты WP_Query

6,530 views

Published on

Published in: Technology
  • WP_Query очень крутая штука, вот я описывал несколько примеров работы с ней: http://web-jenezis.com/2013/11/28/sekrety-wp_query/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Секреты WP_Query

  1. 1. WP_Query
  2. 2. is_home()is_single()is_page()
  3. 3. query_posts()
  4. 4. if ( have_posts() ) while ( have_posts() ) the_post();
  5. 5. query_posts( cat=-5 );$posts = get_posts( cat=-5 );$posts = new WP_Query( cat=-5 );
  6. 6. 10 posts
  7. 7. 10 posts + 10 posts cat = -5
  8. 8. max_num_pages = 3 index.php404.php max_num_pages = 5
  9. 9. pre_get_posts
  10. 10. add_action( pre_get_posts, my_func );function my_func( $query ) { $query->set( cat, -5 );}
  11. 11. $query->is_main_query()
  12. 12. add_action( pre_get_posts, my_func );function my_func( $query ) { if ( $query->is_main_query() ) $query->set( cat, -5 );}
  13. 13. add_action( pre_get_posts, my_func );function my_func( $query ) { if ( $query->is_main_query() ) if ( $query->is_search() ) $query->set( post_type, post );}
  14. 14. add_action( pre_get_posts, my_func );function my_func( $query ) { if ( $query->is_main_query() ) if ( $query->is_search() ) $query->set( posts_per_page, 30 );}
  15. 15. add_action( pre_get_posts, my_func );function my_func( $query ) { if ( $query->is_main_query() ) if ( $query->is_home() ) $query->set( post_type, array( post, book, ) );}
  16. 16. query_posts();$posts = get_posts();$posts = new WP_Query();
  17. 17. $popular = new WP_Query( cat=3 );while ( $popular->have_posts() ) { $popular->the_post(); ...}
  18. 18. $popular = new WP_Query( cat=3 );while ( $popular->have_posts() ) { $popular->the_post(); ...}// Основной запросwhile ( have_posts() ) { the_post(); ...}
  19. 19. // Основной запросwhile ( have_posts() ) { the_post(); ... $category = get_the_category(); $related = new WP_Query( cat= ... ); while ( $related->have_posts() ) { $related->the_post(); ... }}
  20. 20. query_posts()
  21. 21. $wp_query
  22. 22. function have_posts() { global $wp_query; return $wp_query->have_posts();}
  23. 23. $wp_query =& $wp_the_query;
  24. 24. function &query_posts( $query ) { ... unset( $wp_query ); $wp_query = new WP_Query(); return $wp_query->query( $query );}
  25. 25. function wp_reset_query() { ... unset( $wp_query ); $wp_query =& $wp_the_query;}
  26. 26. $popular = new WP_Query( cat=3 );while ( $popular->have_posts() ) { $popular->the_post(); ...}// Основной запросwhile ( have_posts() ) { the_post(); ...}
  27. 27. query_posts( cat=3 );while ( have_posts() ) { the_post(); ...}wp_reset_query();// Основной запросwhile ( have_posts() ) { the_post(); ...}
  28. 28. Если нужно изменить основной запрос pre_get_posts
  29. 29. Если нужен вторичный запросnew WP_Query; get_posts()
  30. 30. Если нужна головная боль query_posts()

×