wordpress在默认情况下可以使用文章的一个自定义字段进行排序,代码如下:
$args = array( 'meta_key' => 'sort', 'orderby' => 'meta_value_num', // 须 WordPress 2.8 及以上版本 'order' => DESC ); $arms = array_merge($args, $wp_query->query); query_posts($arms); while (have_posts()) //循环内容 endwhile;wp_reset_query();
但是有时候我们可能会碰到更加复杂的情况,需要对两个自定义字段进行组合排序,方法如下:
$query = "SELECT wposts.*, wpostmeta1.*, wpostmeta2.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta1, $wpdb->postmeta wpostmeta2 WHERE wposts.ID = wpostmeta1.post_id AND wposts.ID = wpostmeta2.post_id AND wpostmeta1.meta_key = 'date' AND wpostmeta2.meta_key = 'time' ORDER BY wpostmeta1.meta_value ASC, wpostmeta2.meta_value ASC"; $results = $wpdb->get_results($query); foreach ( $results as $result ) { //循环内容 }