[英]Order by custom field (numeric) in WP_Query
I am using following query to get all posts with post_type 'portfolio'. 我正在使用以下查询来获取post_type'portfolio'的所有帖子。
$args = array(
'posts_per_page' => -1,
'offset'=> 0,
'post_type' => 'portfolio'
);
$all_posts = new WP_Query($args);
Where $args
is: $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
);
This should sort the results by up_count. 这应该按up_count对结果进行排序。 But that's not the case.
但事实并非如此。 The codex for wp_query doesn't clearly state about sorting with custom field (or may be I am missing something?).
wp_query的codex没有明确说明使用自定义字段排序(或者可能是我遗漏了什么?)。
This is the query I get when debugging wp_query request. 这是我在调试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
is an extra field of type int in table posts
table. 编辑 :
up_count
是表posts
表中int类型的额外字段。
PS I am using wordpress ver. PS我正在使用wordpress ver。 3.5.2
3.5.2
WP_Query Arguments should be: WP_Query参数应该是:
$args = array(
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'portfolio',
'meta_key' => 'up_count'
'orderby' => 'meta_value_num'
'order' => 'DESC',
);
All this is written in the Codex , but you need to read many times to understand. 所有这些都写在法典中 ,但你需要多次阅读才能理解。
While reviewing the query.php
which actually in action when you call the wp_query
while reviewing the whole cycle for the processing of $args
passed in the wp_query there is the limitation in this method you can only order the posts with the below hardcoded array of fields which is located at line no. 在回顾
query.php
实际上在行动当你调用wp_query
同时查看整个周期的处理$args
在wp_query通过存在这种方法的局限性,只能订购带有字段的下方硬编码阵列的帖子位于第一号线。 2348 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
There is the switch cases for the above array values so if you have altered the wp_posts
table and you want to order the results with this custom field there will be two ways 上面的数组值有切换案例,所以如果您更改了
wp_posts
表并且想要使用此自定义字段对结果进行排序,那么将有两种方法
One way is your filed name should have the prefix post_ like post_up_count and in above array add the additional value like 一种方法是你的文件名应该有post_up_count前缀post_和在上面的数组中添加额外的值
$allowed_keys = array('name', 'author', 'date', 'title','up_count' ,'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
Second is to write the custom query and use $wpdb
class object 第二是编写自定义查询并使用
$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
");
As there are other two more functions to fetch posts like query_posts();
由于还有另外两个函数来获取
query_posts();
等帖子query_posts();
and get_posts()
but these two also uses the wp_query()
和
get_posts()
但这两个也使用wp_query()
Working of query_posts()
使用
query_posts()
function query_posts($query) {
$GLOBALS['wp_query'] = new WP_Query();
return $GLOBALS['wp_query']->query($query);
}
Working of get_posts()
使用
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);
}
So last option is to go with $wpdb
wpdb 所以最后一个选择是使用
$wpdb
wpdb
for examle order users by points 对于按订单分组的用户
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
");
Important : ABS(meta_value) < for Numeric Order 重要提示: ABS(meta_value) <用于数字顺序
this is best way ;) 这是最好的方法;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.