当您在查看 wp_query 中传递的处理的整个周期时,查看query.php
实际在执行的操作时,此方法存在限制,您只能使用以下位于第 no 行的硬编码字段数组对帖子进行排序. 2348wp_query
$args
$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