You can use the pre_get_posts
hook – the following code assumes your input name is event_date
, and the values are in the format YYYY-MM
.
It runs a few checks (is the input non-empty, are we in the admin, is this the main query, and is this for events
?) before generating a meta query, which is an SQL IN
for all the possible dates within the specified month:
add_action( 'pre_get_posts', function ( $wp_query ) {
if (
! empty( $_REQUEST['event_date'] ) &&
is_admin() &&
$wp_query->is_main_query() &&
$wp_query->get( 'post_type' ) === 'events'
) {
// Assuming $_REQUEST['event_date'] is in the format YYYY-MM
$year_month = $_REQUEST['event_date'];
$dates = [];
for ( $day = 1; $day <= 32; $day++ ) {
$dates[] = "$year_month-$day";
}
$wp_query->set( 'meta_query', [[
'key' => 'event_date',
'compare' => 'IN',
'value' => $dates,
]]);
}
});