pre_get_posts
最后更新于:2021-11-27 18:49:14
do_action_ref_array( ‘pre_get_posts’, GC_Query $query )
Fires after the query variable object is created, but before the actual query is run.
参数
function target_main_category_query_with_conditional_tags( $query ) { if ( ! is_admin() && $query->is_main_query() ) { // Not a query for an admin page. // It's the main query for a front end page of your site. if ( is_category() ) { // It's the main query for a category archive. // Let's change the query for category archives. $query->set( 'posts_per_page', 15 ); } } } add_action( 'pre_get_posts', 'target_main_category_query_with_conditional_tags' );
源文件
文件: gc-includes/class-gc-query.php
/** * * The Code below will modify the main GeChiUI loop, before the queries fired, * to only show posts in the halloween category on the home page. * */ function sbt_exclude_category($query){ if ( $query->is_home() && $query->is_main_query() && ! is_admin() ) { $query->set( 'category_name', 'halloween' ); } } add_action('pre_get_posts','sbt_exclude_category');
function university_adjust_queries($query){ if ( ! is_admin() && is_post_type_archive( 'event' ) && $query->is_main_query() ) { $query->set( 'meta_key', 'event_date' ); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'order', 'ASC'); $query->set( 'meta_query', array( array( 'key' => 'event_date', 'compare' => '>=', 'value' => date('Ymd'), 'type' => 'numeric', ) ) ); } } add_action( 'pre_get_posts', 'university_adjust_queries' );