我有这个查询来显示自定义的post类型“property”,它工作得非常好,但是“orderby”语句被完全忽略了。。。
$args = array(
\'post_type\' => CUSTOM_POST_TYPE1,
\'post_status\' => \'publish\',
\'meta_key\' => \'estado\',
\'meta_value\' => \'Activa\',
\'orderby\'=> \'modified\',
\'order\' => \'DESC\',
\'paged\' => $paged,
\'posts_per_page\' => $posts_per_page,
\'tax_query\' => array(
array(
\'taxonomy\' => \'pcategory\',
\'field\' => \'slug\',
\'terms\' => $current_term->slug
)
)
);
$my_query = new WP_Query($args);
这是由
WP_Query() "$my_query->request"
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (28) )
AND wp_posts.post_type = \'propiedades\'
AND (wp_posts.post_status = \'publish\')
AND ( (wp_postmeta.meta_key = \'estado\' AND CAST(wp_postmeta.meta_value AS CHAR) = \'Activa\') )
GROUP BY wp_posts.ID
ORDER BY (select wp_postmeta.meta_value from wp_postmeta where wp_postmeta.post_id=wp_posts.ID and wp_postmeta.meta_key = \'featured_c\')
ASC,post_date DESC LIMIT 0, 6
是否可以按“post\\u modified”自定义帖子字段排序?
最合适的回答,由SO网友:s_ha_dum 整理而成
有一个ORDER BY
该查询中不应由这些参数生成的子句。此外,它看起来不像WordPress查询--请注意大写。WordPress倾向于将所有大写字母用于SQL函数和运算符。
我怀疑有一个过滤器posts_orderby
或posts_clauses
这就是在不应该改变查询的时候改变查询。当我测试您的查询时ORDER BY
子句是您从这些参数中所期望的--ORDER BY wp_posts.posts_modified
-- 这就为有什么东西把查询搞砸了这一理论提供了依据。
找到修改查询的函数,发布它,我将修改答案。寻找add_filter(\'posts_orderby\'
或add_filter(\'posts_clauses\'
在插件和主题中罢工>
如前所述,该函数覆盖ORDER BY
对于大多数每个查询。很难判断该过滤器应该在何处/何时运行,但将其限制在主查询和类别中应该会有所帮助。
function category_filter_orderby($orderby) {
if (is_main_query() || is_category()) {
global $wpdb;
//return "post_date DESC";
$orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id=$wpdb->posts.ID and $wpdb->postmeta.meta_key = \'featured_c\') ASC,post_date DESC";
}
return $orderby;
}
我不知道那是否应该是手术室(
||
) 或AND(
&&
), 或者是否应该有其他条件。我必须读懂主题设计师的想法才能告诉你。