[英]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.