Use the pre_get_posts
action to override the default query. In the admin, a date query will be the query var m
in the format YYYYMM
. Assuming your meta dates are stored the same as MySQL dates (YYYY-MM-DD
), you can use the following:
function wpse_189824_date_meta_query( $wp_query ) {
if ( is_admin() && $wp_query->is_main_query() && $wp_query->get( 'post_type' ) === 'sermons' ) {
if ( $m = $wp_query->get( 'm' ) ) {
$y = substr( $m, 0, 4 ); // Year
$m = substr( $m, 4, 2 ); // Month
if ( ! $meta_query = $wp_query->get( 'meta_query' ) ) // Keep meta query if there currently is one
$meta_query = array();
// Using LIKE query
$meta_query[] = array(
'key' => 'recorded_date',
'value' => "$y-$m-%",
'compare' => 'LIKE',
);
/*
// OR using DATE
$meta_query[] = array(
'key' => 'recorded_date',
'cast' => 'DATE',
'value' => array( "$y-$m-01", "$y-$m-31" ),
'compare' => 'BETWEEN',
);
*/
$wp_query->set( 'meta_query', $meta_query );
$wp_query->set( 'm', null );
}
}
}
add_action( 'pre_get_posts', 'wpse_189824_date_meta_query' );
I’ve given two options (LIKE
or DATE
), you might want to test and see which gives the best performance.