How to limit wp_get_archives to show months for the X years only

Can anybody show me how to set a limit on wp_get_archives to show archives by month BUT only those for the year 2010 and above – so the 2009 monthly archives are not shown on the sidebar archive widget.

Thank you!

1 Answer
1

If using “regular” query in theme file (sidebar.php for example), this code will help do the trick:

<?php wp_get_archives('type=monthly&limit=12'); ?>

The above (and below) will only show the last 12 months.

However if using a widget, then add this code to your functions.php file:

function my_limit_archives( $args ) {
    $args['limit'] = 12;
    return $args;
}
add_filter( 'widget_archives_args', 'my_limit_archives' );
add_filter( 'widget_archives_dropdown_args', 'my_limit_archives' );

Note: In both of the above code examples, I don’t remember where I got the original code from (Google can probably find it?). I just pulled this from a fix I effected on another theme.

Finally, this resource may help point you in the direction to list archives only from 2010 to the present: http://www.wpbeginner.com/wp-themes/how-to-customize-the-display-of-wordpress-archives-in-your-sidebar/

I hope the above helps you in some way! 🙂

Leave a Comment