Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WP_查询中按自定义字段排序(数字)_Php_Sql_Wordpress - Fatal编程技术网

Php WP_查询中按自定义字段排序(数字)

Php WP_查询中按自定义字段排序(数字),php,sql,wordpress,Php,Sql,Wordpress,我使用下面的查询来获取所有post_类型为“portfolio”的帖子 $args = array( 'posts_per_page' => -1, 'offset'=> 0, 'post_type' => 'portfolio' ); $all_posts = new WP_Query($args); 其中,$args是: $args = array(

我使用下面的查询来获取所有post_类型为“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
             );
这将按递增计数对结果进行排序。但事实并非如此。没有明确说明如何使用自定义字段进行排序(或者可能是我遗漏了什么?)

这是调试wp_查询请求时得到的查询

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\u count
是表
posts
表中int类型的额外字段


另外,我正在使用wordpress版本。3.5.2查询参数应为:

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

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

在查看
query.php
时,当你调用
wp\u查询时,它实际上起作用了。
在查看wp\u查询中传递的
$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\ulikepost\u up\u count,并在上面的数组中添加附加值,如

    $allowed_keys=array('name','author','date','title','up_count','modified','menu order','parent','ID','rand','comment_count')

  • 其次是编写自定义查询并使用
    $wpdb
    类对象

    global$wpdb

    $wpdb->get_results(“选择ap_posts.*从ap_posts,其中1=1和ap_posts.post_type='portfolio'和(ap_posts.post_status='publish'或ap_posts.post_status='private')按ap_posts.up_count DESC排序
    ");

因为还有另外两个函数来获取帖子,比如
query_posts()
get_posts()
但是这两个也使用
wp_query()

处理
query\u posts()

处理
get\u posts()

所以最后一个选项是使用
$wpdb

对于按点数排序的examle order用户

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


这是最好的办法;)

如果您编写自定义查询,或者必须使用wp_查询,该怎么办?自定义查询可以是最后一个选项。我正在定制一个高级主题,如果我能用wp_query做这件事,它将节省我很多时间。谢谢,这正是我所需要的!但我猜它少了几个逗号?:)
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);

}
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
");