我想在archive.php中使用排序
所以用function.php编写这段代码
function change_order_for_links( $query ) {
if ( is_archive() || is_search() ) {
$query->set( 'meta_key', 'pin_archive' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DEC' );
}
}
function change_order_for_links_rate( $query ) {
if ( is_main_query() && ( is_archive() || is_search() )) {
$query->set( 'meta_key', '_wpcr_rating_stars_avg' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DEC' );
}
}
function change_order_for_links_view( $query ) {
if ( is_main_query() && ( is_archive() || is_search() )) {
$query->set( 'meta_key', 'views' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DEC' );
}
}
function set_ordering() {
if ( isset( $_GET['order'] ) ) {
$order = $_GET['order'];
if ( $order == 'view' ) {
add_action( 'pre_get_posts', 'change_order_for_links_view' );
} elseif ( $order == 'rate' ) {
add_action( 'pre_get_posts', 'change_order_for_links_rate' );
}
} else {
add_action( 'pre_get_posts', 'change_order_for_links' );
}
}
add_action( 'pre_get_posts', 'set_ordering' );但此设置适用于存档页面中的所有查询
所有查询的is_main_query()值均为true
https://stackoverflow.com/questions/51574725
复制相似问题