是按一个自定义字段筛选,还是按另一个自定义字段排序?

时间:2011-02-17 作者:gillespieza

我有一个自定义的帖子类型“Listing”,我想获取所有具有自定义字段的列表gateway_value != \'Yes\', 并按另一个自定义字段对结果排序,location_level1_value. 我可以让查询单独工作,但不能将它们组合起来:

查询1(按位置排序):

                $wp_query = new WP_Query( array (
                    \'post_type\' => \'listing\',
                    \'post_status\' => \'publish\',
                    \'posts_per_page\' => \'9\',
                    \'meta_key\' => \'location_level1_value\',
                    \'orderby\' => \'location_level1_value\',
                    \'order\' => \'ASC\',
                    \'paged\' => $paged
                    )
                 );
查询2(自定义字段值!=是):
                $wp_query = new WP_Query( array (
                    \'post_type\' => \'listing\',
                    \'posts_per_page\' => \'9\',
                    \'post_status\' => \'publish\',
                    \'meta_key\' => \'gateway_value\',
                    \'meta_value\' => \'Yes\',
                    \'meta_compare\' => \'!=\',
                    \'paged\' => $paged
                    )
                );
组合查询:我查看了codex 有关此方面的帮助,但以下查询不起作用:

                $wp_query = new WP_Query( array (
                    \'post_type\' => \'listing\',
                    \'posts_per_page\' => \'9\',
                    \'post_status\' => \'publish\',
                    \'meta_query\' => array(
                        array(
                            \'key\' => \'gateway_value\',
                            \'value\' => \'Yes\',
                            \'compare\' => \'!=\'
                        ),
                        array(
                            \'key\' => \'location_level1_value\'
                        )
                    ),
                    \'orderby\' => "location_level1_value",
                    \'order\' => \'ASC\',
                    \'paged\' => $paged
                    )
                );
组合查询有什么错?

[更新]:所以现在3.1已经发布,上面的组合查询仍然不起作用。我确实得到了结果,只是排序不正确。

[更新]:var_dump($wp_query->request) 给出以下内容:
string(527) " SELECT SQL_CALC_FOUND_ROWS wp_7v1oev_posts.* FROM wp_7v1oev_posts INNER JOIN wp_7v1oev_postmeta ON (wp_7v1oev_posts.ID = wp_7v1oev_postmeta.post_id) INNER JOIN wp_7v1oev_postmeta AS mt1 ON (wp_7v1oev_posts.ID = mt1.post_id) WHERE 1=1 AND wp_7v1oev_posts.post_type = \'listing\' AND (wp_7v1oev_posts.post_status = \'publish\') AND wp_7v1oev_postmeta.meta_key = \'gateway_value\' AND CAST(wp_7v1oev_postmeta.meta_value AS CHAR) != \'Yes\' AND mt1.meta_key = \'location_level1_value\' ORDER BY wp_7v1oev_posts.post_date DESC LIMIT 0, 9"

3 个回复
最合适的回答,由SO网友:Miljenko Barbir 整理而成

通过使用带有过滤选项的“meta\\u查询”,您可以使用该查询按预期过滤内容,对于订单部分,只需添加/修改以下参数即可:

\'orderby\'=>\'meta\\u value\'\'meta\\u key\'=>\'location\\u level1\\u value\'\'order\'=>\'ASC\'

$wp_query = new WP_Query( array (
    \'post_type\'      => \'listing\',
    \'posts_per_page\' => \'9\',
    \'post_status\'    => \'publish\',
    \'meta_query\'     => array(
        array(
            \'key\'       => \'gateway_value\',
            \'value\'     => \'Yes\',
            \'compare\'   => \'!=\'
        )
    ),
    \'orderby\'  => \'meta_value\',            // this means we will be using a selected 
                                           // meta field to order

    \'meta_key\' => \'location_level1_value\', // this states which meta field 
                                           // will be used in the ordering, 
                                           // regardless of the filters
    \'order\'    => \'ASC\',
    \'paged\'    => $paged
    )
);

SO网友:Bainternet

正如Jan在新WordPress 3.1中所说,您可以使用meta_query 但在这之前,您可以使用第一个查询对orderby进行查询,并在循环中进行筛选,如下所示:

 Global $my_query;
$my_query = new WP_Query( array (
                    \'post_type\' => \'listing\',
                    \'post_status\' => \'publish\',
                    \'posts_per_page\' => \'9\',
                    \'meta_key\' => \'location_level1_value\',
                    \'orderby\' => \'location_level1_value\',
                    \'order\' => \'ASC\',
                    \'paged\' => $paged
                    )
                 );
while ($my_query->have_posts){
    $my_query->the_post();
              //do your loop stuff
} 
并将此代码添加到函数中。php

   //join filter
         add_filter(\'posts_join\', \'listing_join_865\' );
         function listing_join_865($join){
Global$ my_query;            
if (\'listing\' = $my_query->query[\'post_type\']){
                $restriction1 = \'gateway_value\';
                return $join .="
                LEFT JOIN $wpdb->postmeta AS $restriction1 ON(
                $wpdb->posts.ID = $restriction1.post_id
                AND $restriction1.meta_key = \'$restriction1\'
                )";
             }else {
                return $join;
            }
         }
         //where filter
         add_filter(\'posts_where\', \'listing_where_865\' );
         function listing_where_865($where){
             global $my_query;
            if (\'listing\' = $my_query->query[\'post_type\']){
                return $where.= " AND $restriction1.meta_value != \'yes\'";
            }else{
                return $where;
            }
         }
现在应该可以了。

SO网友:gillespieza

很抱歉回答我自己的问题:

正在查看[http://core.trac.wordpress.org/ticket/15031][1] ,似乎这是一个已知的问题。我已经修复(黑客?)使用it工作post_filter, 就像这样(仅供可能正在搜索相同答案的任何人参考):

在函数中。php###

add_filter(\'posts_orderby\', \'EV_locationl1\' );
function EV_locationl1 ($orderby) {
    global $EV_locationl1_orderby;
    if ($EV_locationl1_orderby) $orderby = $EV_locationl1_orderby;
    return $orderby;
}
修改了模板文件中的wp\\U查询###
$EV_locationl1_orderby = " mt1.meta_value ASC";

$wp_query = new WP_Query( array (
    \'post_type\' => \'listing\',
    \'posts_per_page\' => \'9\',
    \'post_status\' => \'publish\',
    \'meta_query\' => array(
            array(
                    \'key\' => \'gateway_value\',
                    \'value\' => \'Yes\',
                    \'compare\' => \'!=\'
                    ),
            array(
                    \'key\' => \'location_level1_value\'
            )
        ),
    \'order\' => $EV_locationl1_orderby,
    \'paged\' => $paged
    ));

结束

相关推荐