I've modified this post a few times, asking for help. And I finally got it figured out, so I thought I would share what worked for me.
The goal was to create a Meta Query in Wordpress on a Custom Date Field, in a custom post type. Not the WP Core post date. At first I wanted to display the month and the year date with previous
and next
buttons that would advance to the next month. But I gave up on that and thought it would work to query months and display them separately, possibly using html accordion tabs.
Thanks to @Sim1-81
using AND (wpostmeta.meta_value BETWEEN '2020-03-01 00:00:01' AND '2020-03-31 23:59:59')
- worked, for March of 2020. But I needed the year to be based on the "current year." Pulling the month and date this way was fine, but I wanted the year to change so it pulled data from 1. March of current year - or, 2. March of the following year, if it had passed (April, etc).
This code does exactly that:
if($n>3){
$y = date('Y') + 1;
} else {
$y = date('Y');
}
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'custom_date_field'
AND (wpostmeta.meta_value BETWEEN '".$y."-03-01 00:00:01' AND '".$y."-03-31 23:59:59')
AND wposts.post_status = 'publish'
AND wposts.post_type = 'event'
ORDER BY wpostmeta.meta_value DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT_K);
?>```