Skip to content

Instantly share code, notes, and snippets.

@gzalinski
Created August 29, 2022 16:58
Show Gist options
  • Save gzalinski/6f15f5555512d52331b6b00de22b0380 to your computer and use it in GitHub Desktop.
Save gzalinski/6f15f5555512d52331b6b00de22b0380 to your computer and use it in GitHub Desktop.
WP_Query: Posts between two meta date
global $wpdb;
$today_start = date('Y-m-d H:i:s', strtotime('yesterday 9pm'));
$today_end = date('Y-m-d H:i:s', strtotime('today 9pm'));
$SQL_ACF_TODAY = "SELECT $wpdb->posts.id FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta
ON $wpdb->postmeta.post_id = $wpdb->posts.ID
WHERE 1 = 1
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = 'wod_date'
AND CAST($wpdb->postmeta.meta_value AS DATETIME) BETWEEN '$today_start' AND '$today_end'
LIMIT 1";
//get post where wod_date = today
$result = $wpdb->get_var($SQL_ACF_TODAY);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment