通过一个自定义字段过滤,按另一个定制字段订购?
-
16-10-2019 - |
题
我有一个自定义帖子类型“列表”,我想获得具有自定义字段的所有列表 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(自定义字段值!= yes):
$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
)
);
合并查询:
我看着 法典 为此提供帮助,但是以下查询不起作用:
$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"
解决方案
您可以使用查询来通过使用过滤选项的“ meta_query”来过滤内容,并且对于顺序零件,只需添加/修改以下参数:
- 'orderby'=>'meta_value'
- 'meta_key'=>'location_level1_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 ) );
其他提示
就像Jan在新的WordPress 3.1中所说的那样,您可以使用 meta_query
但是,在此之前,您可以使用第一个查询来订购和过滤您的循环中:
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
}
并将此代码添加到您的functions.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;
}
}
现在这应该起作用。
很抱歉回答我自己的问题:
查看[http://core.trac.wordpress.org/ticket/15031] [1],这似乎是一个已知的问题。我已经修复了(黑客?)它可以使用 post_filter
, ,像这样(仅适用于任何可能正在寻找相同答案的人的参考):
在functions.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_query ###
$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
));