我尝试了以下SQL查询,但没有成功。任何对SQL有更好理解的人都能提供帮助吗?
以下是我正在努力实现的目标:
获取父项id为0的所有主要事件(ME)的列表根据从子查询获得的最早子事件的发布日期对ME列表进行排序我不知道如何在默认的WP\\u查询对象中获得相同的结果。
SQL代码:
SELECT * FROM `wp_posts` AS ME LEFT JOIN
(
SELECT * FROM `wp_posts` AS SE
WHERE SE.`post_type`="event"
AND SE.`post_status` IN ("publish", "future")
AND SE.`post_parent` != 0
GROUP BY SE.`post_parent`
ORDER BY SE.`post_date` ASC
LIMIT 1
) AS SE2
ON ME.ID = SE2.post_parent
WHERE ME.post_parent = 0
AND ME.post_type="event"
AND ME.post_status IN("publish", "future")
ORDER BY SE2.post_date
将使用它的代码。function pre_get_posts($query) {
global $wpdb;
if(!is_admin() && $query->is_main_query()) {
//check if the post type matches this post type
if(is_post_type_archive(\'event\')) {
//will this work?
$query->set(\'query\', \'SELECT * FROM `wp_posts` AS ME LEFT JOIN (SELECT * FROM `wp_posts` AS SE WHERE SE.`post_type`="event" AND SE.`post_status` IN ("publish", "future") AND SE.`post_parent` != 0 GROUP BY SE.`post_parent` ORDER BY SE.`post_date` ASC LIMIT 1) AS SE2 ON ME.ID = SE2.post_parent WHERE ME.post_parent = 0 AND ME.post_type="event" AND ME.post_status IN("publish", "future") ORDER BY SE2.post_date\');
}
}
}