Секреты wp_query

32
WP_Query

Upload: konstantin-kovshenin

Post on 18-Jun-2015

4.666 views

Category:

Technology


10 download

TRANSCRIPT

Page 1: Секреты WP_Query

WP_Query

Page 2: Секреты WP_Query

is_home()is_single()is_page()

Page 3: Секреты WP_Query

query_posts()

Page 4: Секреты WP_Query

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

Page 5: Секреты WP_Query

query_posts( 'cat=-5' );$posts = get_posts( 'cat=-5' );$posts = new WP_Query( 'cat=-5' );

Page 6: Секреты WP_Query

10 posts

Page 7: Секреты WP_Query

10 posts 10 postscat = -5+

Page 8: Секреты WP_Query

max_num_pages = 3

404.php index.phpmax_num_pages = 5

Page 9: Секреты WP_Query

pre_get_posts

Page 10: Секреты WP_Query

add_action( 'pre_get_posts', 'my_func' );function my_func( $query ) {$query->set( 'cat', '-5' );

}

Page 11: Секреты WP_Query

$query->is_main_query()

Page 12: Секреты WP_Query

add_action( 'pre_get_posts', 'my_func' );function my_func( $query ) {if ( $query->is_main_query() )$query->set( 'cat', '-5' );

}

Page 13: Секреты WP_Query

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

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

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

query_posts();$posts = get_posts();$posts = new WP_Query();

Page 17: Секреты WP_Query

$popular = new WP_Query( 'cat=3' );while ( $popular->have_posts() ) {$popular->the_post();...

}

Page 18: Секреты WP_Query

$popular = new WP_Query( 'cat=3' );while ( $popular->have_posts() ) {$popular->the_post();...

}

// Основной запросwhile ( have_posts() ) {the_post();...

}

Page 19: Секреты WP_Query

// Основной запрос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

query_posts()

Page 21: Секреты WP_Query

$wp_query

Page 22: Секреты WP_Query

function have_posts() {global $wp_query;return $wp_query->have_posts();

}

Page 23: Секреты WP_Query

$wp_query =& $wp_the_query;

Page 24: Секреты WP_Query

function &query_posts( $query ) {...unset( $wp_query );$wp_query = new WP_Query();return $wp_query->query( $query );

}

Page 25: Секреты WP_Query

function wp_reset_query() {...unset( $wp_query );$wp_query =& $wp_the_query;

}

Page 26: Секреты WP_Query

$popular = new WP_Query( 'cat=3' );while ( $popular->have_posts() ) {$popular->the_post();...

}

// Основной запросwhile ( have_posts() ) {the_post();...

}

Page 27: Секреты WP_Query

query_posts( 'cat=3' );while ( have_posts() ) {the_post();...

}wp_reset_query();

// Основной запросwhile ( have_posts() ) {the_post();...

}

Page 28: Секреты WP_Query

pre_get_posts

Если нужно изменить основной запрос

Page 29: Секреты WP_Query

new WP_Query; get_posts()

Если нужен вторичный запрос

Page 30: Секреты WP_Query

query_posts()

Если нужна головная боль

Page 31: Секреты WP_Query
Page 32: Секреты WP_Query