global $wpdb;
$sql = "SELECT * FROM $wpdb->postmeta";
$sql .= " INNER JOIN $wpdb->posts ON $wpdb->postmeta.post_id = $wpdb->posts.ID";
$sql .= " WHERE $wpdb->posts.post_type = 'task'";
$sql .= " AND $wpdb->posts.post_parent IN ( SELECT `ID` FROM $wpdb->posts WHERE `post_type` = 'task_list' AND `post_parent` IN ( SELECT `ID` FROM $wpdb->posts WHERE `post_status` = 'publish' AND `post_type` = 'project' ) )";
$sql .= " AND $wpdb->postmeta.post_id IN ( SELECT `post_id` FROM $wpdb->postmeta WHERE $wpdb->postmeta.meta_key = '_completed' AND $wpdb->postmeta.meta_value = '0' )";
$sql .= " AND $wpdb->postmeta.post_id IN ( SELECT `post_id` FROM $wpdb->postmeta WHERE $wpdb->postmeta.meta_key = '_assigned' AND $wpdb->postmeta.meta_value = '%s' )";
$sql .= " AND $wpdb->postmeta.meta_key = '_due'";
$sql .= " GROUP BY $wpdb->posts.ID";
$sql .= " ORDER BY CAST( $wpdb->postmeta.meta_value AS DATE ) ASC, $wpdb->posts.ID ASC";
$tasks = $wpdb->get_results( sprintf( $sql, $user_id ) );
return $tasks;
查询的结果是这样的:
- 帖子 A。
- 发布 B。
- 帖子 X. 截止日期:8 月 2 日
- Y 后到期:8 月 3 日
- Z 后到期:8 月 4 日
为什么没有_due
价值的帖子总是先返回?我怎样才能做到让它们总是最后返回?如果我这样做:
$sql .= " ORDER BY $wpdb->posts.ID ASC, CAST( $wpdb->postmeta.meta_value AS DATE ) ASC";
没有_due
价值的帖子随机放置在有价值的帖子之间_due
。
理想的结果应该是:
- 帖子 X. 截止日期:8 月 2 日
- Y 后到期:8 月 3 日
- Z 后到期:8 月 4 日
- 帖子 A。
- 发布 B。
想法?