Not refresh archive month filter

Question

So I’m using wp_get_archives in order to show a list of years, together with months limited to each year (so instead of showing the 12 months for each year it only shows the months once and they work as links within the years – You first click 2020, then January -> /2020/01 and so on). So you basically click on a link first and then navigate through the months for each year.

With the help of another user I managed to make this work. But when I navigate through the months, although the links work perfectly the ‘navigation’ seems to refresh, so I can go go to 2019/03 without problem but when on a month archive, the month links reset to the current year (2020).

Is there a workaround on that? Is it possible to keep the selected month as current month selected instead of refreshing to the new one?

Here’s the code in date.php:

        <div class="archive-all">
                    <ul class="year">
                        <?php

                        wp_get_archives( [
                        'type'            => 'yearly',
                        'format'          => 'html',
                        'show_post_count' => 0,
                        ] );

                        ?>
                                </ul>
                    <ul class="month">
                        <?php

                  wp_get_archives( [
                             'type'            => 'monthly',
                             'year'            => is_year() ? get_query_var( 'year' ) : date('Y'),
                             'format'          => 'html',
                             'show_post_count' => 0,
                     'order'           => 'ASC',
                     'echo'            => 1,
                     ] );


                        ?>
                                </ul>
        </div>

And here’s the one in functions.php:

add_filter( 'getarchives_where', 'wp_get_archives_month_filter', 10, 2 );
function wp_get_archives_month_filter($where, $args){
        if ( 'monthly' !== $args['type'] ) {
                return $where;
        }
        $year = $args['year'];
        $start = date( 'Y-m-d', strtotime( $year . '-01-01' ) );
        $end = date( 'Y-m-d', strtotime( $year . '-12-31' ) );
        $where.= ' AND `post_date` BETWEEN "' . $start . '" AND "'. $end . '"';
        return $where;
}
0
Anta 4 months 2020-12-21T05:10:37-05:00 0 Answers 6 views 0

Leave an answer

Browse
Browse