0

我正在为 wp_query 苦苦挣扎,我需要你的帮助。

我有 3 个自定义字段,分别称为“Agenda_day”、“Agenda_month”、“Agenda_year”,分别代表事件的日、月和年。

我想按天、按月、最后按年对查询结果进行升序排列。

这是我的查询:

$query_agenda = new WP_Query(
    array(
        'posts_per_page' => 8, 
        'cat' => 4, 
        'meta_query' => 
            array(
                'relation' => 'AND', 
                'day' => array('key' => 'Agenda_day', 'compare' => 'EXISTS'), 
                'month' => array('key' => 'Agenda_month', 'compare' => 'EXISTS'), 
                'year' => array('key' => 'Agenda_year', 'compare' => 'EXISTS')
            ), 
        'orderby' => array('day' => 'ASC', 'month' => 'ASC', 'year' => 'ASC')
    )
);

这不起作用......你能解释一下为什么并告诉我如何解决它吗?

谢谢 !

编辑

这是执行的查询(返回结果但排序不正确)

从 wp_posts 中选择 SQL_CALC_FOUND_ROWS wp_posts.ID 左连接 wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = INNER JOIN). wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) WHERE 1=1 AND (wp_term_relationships.term_taxonomy_id IN (4)) AND (wp_postmeta.meta_key = 'Agenda_jour' AND mt1.meta_key = 'Agenda_mois' AND mt2.meta_key = ' Agenda_annee' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY CAST(wp_postmeta.meta_value AS CHAR) ASC, CAST(mt1. meta_value AS CHAR) ASC, CAST(mt2.meta_value AS CHAR) ASC LIMIT 0, 8

4

2 回答 2

1

https://codex.wordpress.org/Class_Reference/WP_Query https://codex.wordpress.org/Class_Reference/WP_Meta_Query

请检查以上两个链接,您将得到答案和您传递的 orderby 参数是错误的

于 2018-04-16T11:50:38.197 回答
0

问题解决了 !

  • 我在自定义字段中处理不同的格式:有些日子和月份是 1 个字符(“6”),有些是两个字符(“06”),所以系统无法很好地排序

  • 第二点是,当我处理日期时,我必须将“orderby”指令从“日、月、年”重新排序为“年、月、日”

于 2018-04-17T08:15:21.473 回答