Q:

Get custom post archived by meta field value in wordpress

I am using cpt-archive plugin to generate archive by month in my wordpress post. Here the plugin using this code, 
$query = "SELECT YEAR( post_date ) AS `year`, 
    MONTH(post_date) AS `month`, 
    count(ID) as posts 
    FROM $wpdb->posts $join $where 
    GROUP BY YEAR(post_date), 
    MONTH(post_date) 
    ORDER BY post_date DESC $limit";

$arcresults = $wpdb->get_results($query);

Here it's working fine with post_date. I need to customize this to get archived by meta fields. I have two meta keys such as _event_start_date and _event_end_date.
edit
now i am getting all the custom post type months based on the _event_end _date with the following query
$months = $wpdb->get_results("SELECT DISTINCT YEAR(meta_value) AS year, MONTH(meta_value) AS month FROM $wpdb->postmeta WHERE meta_key = '_event_end_date' WHERE meta_value <= now( )ORDER BY meta_value ASC");

but i want only past months only. how to do change this query for getting only archived months.

A:

Why don't you use wp_get_archives?
Something like this
<?php wp_get_archives( array( 'type' => 'monthly', 'meta_value' => array('_event_start_date', '_event_end_date')  ) ); ?>

