I have a mini project on the archive. I want to custom the wp_get_archive to get all the available post in months within a selected year. let say I pass in the parameter “year” how do I change the following code so that it return only all the months(post count) belong to that year?

if ( 'monthly' == $r['type'] ) {
    $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 $order $limit";
    $key = md5( $query );
    $key = "wp_get_archives:$key:$last_changed";
    if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
        $results = $wpdb->get_results( $query );
        wp_cache_set( $key, $results, 'posts' );
    }
    if ( $results ) {
        $after = $r['after'];
        foreach ( (array) $results as $result ) {
            $url = get_month_link( $result->year, $result->month );
            if ( 'post' !== $r['post_type'] ) {
                $url = add_query_arg( 'post_type', $r['post_type'], $url );
            }
            /* translators: 1: month name, 2: 4-digit year */
            $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
            if ( $r['show_post_count'] ) {
                $r['after'] = ' (' . $result->posts . ')' . $after;
            }
            $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
        }
    }
}

Read more here: How to modify the this part of wp_get_archive to get only month and post count with an args of year


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: