4

我正在使用以下查询来获取所有带有 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  

编辑up_count是表中 int 类型的额外字段posts

PS我正在使用wordpress版本。3.5.2

4

3 回答 3

13

WP_Query 参数应该是:

$args = array( 
    'posts_per_page' => -1, 
    'offset'         => 0,    
    'post_type'      => 'portfolio',
    'meta_key'       => 'up_count',
    'orderby'        => 'meta_value_num',
    'order'          => 'DESC'
);

所有这些都写在Codex中,但是你需要阅读很多遍才能理解。

于 2013-08-10T17:44:16.863 回答
1

当您在查看 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

于 2013-08-10T18:43:45.167 回答
-1

例如按点排序用户

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) < 用于数字顺序

这是最好的方法;)

于 2014-01-28T22:10:33.937 回答