[英]Order by custom field (numeric) in WP_Query
我正在使用以下查詢來獲取post_type'portfolio'的所有帖子。
$args = array(
'posts_per_page' => -1,
'offset'=> 0,
'post_type' => 'portfolio'
);
$all_posts = new WP_Query($args);
$args
是:
$args = array(
'posts_per_page' => -1,
'offset'=> 0,
'post_type' => 'portfolio',
'orderby' => 'up_count', //up_count is numeric field from posts table
'order' => DESC
);
這應該按up_count對結果進行排序。 但事實並非如此。 wp_query的codex沒有明確說明使用自定義字段排序(或者可能是我遺漏了什么?)。
這是我在調試wp_query請求時得到的查詢。
SELECT ap_posts.* FROM ap_posts WHERE 1=1 AND ap_posts.post_type = 'portfolio' AND (ap_posts.post_status = 'publish' OR ap_posts.post_status = 'private') ORDER BY ap_posts.post_date DESC
編輯 : up_count
是表posts
表中int類型的額外字段。
PS我正在使用wordpress ver。 3.5.2
WP_Query參數應該是:
$args = array(
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'portfolio',
'meta_key' => 'up_count'
'orderby' => 'meta_value_num'
'order' => 'DESC',
);
所有這些都寫在法典中 ,但你需要多次閱讀才能理解。
在回顧query.php
實際上在行動當你調用wp_query
同時查看整個周期的處理$args
在wp_query通過存在這種方法的局限性,只能訂購帶有字段的下方硬編碼陣列的帖子位於第一號線。 2348
$allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
if ( ! in_array($orderby, $allowed_keys) )
continue;
// here your order by fails
上面的數組值有切換案例,所以如果您更改了wp_posts
表並且想要使用此自定義字段對結果進行排序,那么將有兩種方法
一種方法是你的文件名應該有post_up_count前綴post_和在上面的數組中添加額外的值
$allowed_keys = array('name', 'author', 'date', 'title','up_count' ,'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
第二是編寫自定義查詢並使用$wpdb
類對象
global $wpdb;
$wpdb->get_results("SELECT ap_posts.* FROM ap_posts WHERE 1=1 AND ap_posts.post_type = 'portfolio' AND (ap_posts.post_status = 'publish' OR ap_posts.post_status = 'private') ORDER BY ap_posts.up_count DESC
");
由於還有另外兩個函數來獲取query_posts();
等帖子query_posts();
和get_posts()
但這兩個也使用wp_query()
使用query_posts()
function query_posts($query) {
$GLOBALS['wp_query'] = new WP_Query();
return $GLOBALS['wp_query']->query($query);
}
使用get_posts()
function get_posts($args = null) {
$defaults = array(
'numberposts' => 5, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true
);
$r = wp_parse_args( $args, $defaults );
if ( empty( $r['post_status'] ) )
$r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
$r['posts_per_page'] = $r['numberposts'];
if ( ! empty($r['category']) )
$r['cat'] = $r['category'];
if ( ! empty($r['include']) ) {
$incposts = wp_parse_id_list( $r['include'] );
$r['posts_per_page'] = count($incposts); // only the number of posts included
$r['post__in'] = $incposts;
} elseif ( ! empty($r['exclude']) )
$r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
$r['ignore_sticky_posts'] = true;
$r['no_found_rows'] = true;
$get_posts = new WP_Query;
return $get_posts->query($r);
}
所以最后一個選擇是使用$wpdb
wpdb
對於按訂單分組的用戶
global $wpdb;
$order = $wpdb->get_results("
SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key='userpoint' ORDER BY ABS(meta_value) DESC", "ARRAY_N
");
重要提示: ABS(meta_value) <用於數字順序
這是最好的方法;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.