我使用以下查询来获取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的编码没有明确说明使用自定义字段进行排序(或者可能是我遗漏了什么?)。
这是我在调试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
EDIT:up_count
是posts
表中int类型的额外字段。
附言:我使用的是wordpress版本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'
);
所有这些都写在法典中,但你需要多次阅读才能理解。
在查看调用wp_query
时实际起作用的query.php
时,同时查看wp_query中传递的$args
的整个处理周期。此方法有局限性,您只能使用位于第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_,如post_up_count并在上面的数组中添加附加值,如
$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();
和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
对于按点排列的examle订单用户
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)<对于数字订单
这是最好的方式;)