Все секреты SEO-компаний и технология выбора качественного подрядчика
Секреты WP_Query
-
Upload
konstantin-kovshenin -
Category
Technology
-
view
4.644 -
download
10
Transcript of Секреты WP_Query
![Page 1: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/1.jpg)
WP_Query
![Page 2: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/2.jpg)
is_home()is_single()is_page()
![Page 3: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/3.jpg)
query_posts()
![Page 4: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/4.jpg)
if ( have_posts() )while ( have_posts() )the_post();
![Page 5: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/5.jpg)
query_posts( 'cat=-5' );$posts = get_posts( 'cat=-5' );$posts = new WP_Query( 'cat=-5' );
![Page 6: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/6.jpg)
10 posts
![Page 7: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/7.jpg)
10 posts 10 postscat = -5+
![Page 8: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/8.jpg)
max_num_pages = 3
404.php index.phpmax_num_pages = 5
![Page 9: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/9.jpg)
pre_get_posts
![Page 10: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/10.jpg)
add_action( 'pre_get_posts', 'my_func' );function my_func( $query ) {$query->set( 'cat', '-5' );
}
![Page 11: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/11.jpg)
$query->is_main_query()
![Page 12: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/12.jpg)
add_action( 'pre_get_posts', 'my_func' );function my_func( $query ) {if ( $query->is_main_query() )$query->set( 'cat', '-5' );
}
![Page 13: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/13.jpg)
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' );
}
![Page 14: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/14.jpg)
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 );
}
![Page 15: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/15.jpg)
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',
) );}
![Page 16: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/16.jpg)
query_posts();$posts = get_posts();$posts = new WP_Query();
![Page 17: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/17.jpg)
$popular = new WP_Query( 'cat=3' );while ( $popular->have_posts() ) {$popular->the_post();...
}
![Page 18: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/18.jpg)
$popular = new WP_Query( 'cat=3' );while ( $popular->have_posts() ) {$popular->the_post();...
}
// Основной запросwhile ( have_posts() ) {the_post();...
}
![Page 19: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/19.jpg)
// Основной запросwhile ( have_posts() ) {the_post();...
$category = get_the_category();$related = new WP_Query( 'cat=' ... );while ( $related->have_posts() ) {$related->the_post();...
}}
![Page 20: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/20.jpg)
query_posts()
![Page 21: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/21.jpg)
$wp_query
![Page 22: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/22.jpg)
function have_posts() {global $wp_query;return $wp_query->have_posts();
}
![Page 23: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/23.jpg)
$wp_query =& $wp_the_query;
![Page 24: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/24.jpg)
function &query_posts( $query ) {...unset( $wp_query );$wp_query = new WP_Query();return $wp_query->query( $query );
}
![Page 25: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/25.jpg)
function wp_reset_query() {...unset( $wp_query );$wp_query =& $wp_the_query;
}
![Page 26: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/26.jpg)
$popular = new WP_Query( 'cat=3' );while ( $popular->have_posts() ) {$popular->the_post();...
}
// Основной запросwhile ( have_posts() ) {the_post();...
}
![Page 27: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/27.jpg)
query_posts( 'cat=3' );while ( have_posts() ) {the_post();...
}wp_reset_query();
// Основной запросwhile ( have_posts() ) {the_post();...
}
![Page 28: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/28.jpg)
pre_get_posts
Если нужно изменить основной запрос
![Page 29: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/29.jpg)
new WP_Query; get_posts()
Если нужен вторичный запрос
![Page 30: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/30.jpg)
query_posts()
Если нужна головная боль
![Page 31: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/31.jpg)
![Page 32: Секреты WP_Query](https://reader034.fdocuments.us/reader034/viewer/2022042514/5581b192d8b42a1d1a8b46e6/html5/thumbnails/32.jpg)