SELECT wp_posts.*, wp_p2p.*
FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_p2p
WHERE 1=1 AND (
( wp_postmeta.meta_key = 'start_date' AND CAST(wp_postmeta.meta_value AS DATE) >= '2024-10-14' )
) AND ((wp_posts.post_type = 'schedule' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled'))) AND (wp_p2p.p2p_type = 'schedule_to_courses' AND wp_posts.ID = wp_p2p.p2p_from AND wp_p2p.p2p_to IN (SELECT wp_posts.ID
FROM wp_posts
WHERE 1=1 AND wp_posts.ID IN (17014) AND ((wp_posts.post_type = 'courses' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled')))
ORDER BY wp_posts.post_date DESC
))
GROUP BY wp_posts.ID
ORDER BY CAST(wp_postmeta.meta_value AS DATE) ASC