I just want a simple list with the monthly archives for one specific year. Like so:
- January
- February
- March
- April
- etc.
I don't need anything fancy like yearly grouping. Ideally I would like a shortcode like this [month-archive year=2019]. This is how far I've come but the 'Year' parameter does not seem to do anything.
function my_monthly_archives() {
$my_archives = wp_get_archives(array(
'type'=>'monthly',
'limit'=>10,
'echo'=>0,
'year'=>'2018'
));
return $my_archives;
}
// Create a shortcode
add_shortcode('month-archive', 'my_monthly_archives');
// Enable shortcode execution in text widget
add_filter('widget_text', 'do_shortcode');
I just want a simple list with the monthly archives for one specific year. Like so:
- January
- February
- March
- April
- etc.
I don't need anything fancy like yearly grouping. Ideally I would like a shortcode like this [month-archive year=2019]. This is how far I've come but the 'Year' parameter does not seem to do anything.
function my_monthly_archives() {
$my_archives = wp_get_archives(array(
'type'=>'monthly',
'limit'=>10,
'echo'=>0,
'year'=>'2018'
));
return $my_archives;
}
// Create a shortcode
add_shortcode('month-archive', 'my_monthly_archives');
// Enable shortcode execution in text widget
add_filter('widget_text', 'do_shortcode');
Share
Improve this question
asked Mar 28, 2020 at 23:24
Josef UlanderJosef Ulander
434 bronze badges
1 Answer
Reset to default 4The year
, monthnum
, day
and w
(week) parameters are only used to highlight/pre-select the current item in the list/output and they only take effect on an archive page like date and category archives. And by "highlight", I mean WordPress will add either selected='selected'
(if the format
is option
) or aria-current="page"
(for other format
than link
) to the output. Something like this (note the selected
attribute):
// Assume $results contains database's rows (row objects).
// And '2018' is in the $args you pass to wp_get_archives().
$output = '';
foreach ( $results as $row ) {
if ( is_archive() && '2018' === $row->year ) {
$output .= '<option value="2018" selected>2018</option>';
} else {
$output .= '<option value="2018">2018</option>';
}
}
Try the following and visit any archive pages, and you'd see the year 2018
option is pre-selected:
$my_archives = '<select>' . wp_get_archives( array(
'type' => 'yearly',
'limit' => 10,
'echo' => 0,
'year' => '2018',
'format' => 'option', // this outputs an <option>
) ) . '</select>';
Other examples below, assuming there's a (public) post on November 1, 2018:
And that you're on an archive page — on other pages like single posts, those four parameters (year
, monthnum
, day
and w
) default to the ones in the main query.
When
format
ismonthly
,November 2018
would be pre-selected:$my_archives = '<select>' . wp_get_archives( array( 'type' => 'monthly', 'limit' => 10, 'echo' => 0, 'year' => '2018', 'format' => 'option', 'monthnum' => 11, ) ) . '</select>';
When
format
isweekly
,October 29, 2018–November 4, 2018
(week 44th in 2018) would be pre-selected:$my_archives = '<select>' . wp_get_archives( array( 'type' => 'weekly', 'limit' => 10, 'echo' => 0, 'year' => '2018', 'format' => 'option', 'w' => 44, ) ) . '</select>';
When
format
isdaily
,November 1, 2018
would be pre-selected:$my_archives = '<select>' . wp_get_archives( array( 'type' => 'daily', 'limit' => 10, 'echo' => 0, 'year' => '2018', 'format' => 'option', 'monthnum' => 11, 'day' => 1, ) ) . '</select>';
So that year
parameter won't give you the results you're wanting to have; however, it is possible to achieve the results using the getarchives_where
hook in combination with a custom parameter, like so:
This should go in the theme's
functions.php
file:add_filter( 'getarchives_where', function ( $where, $parsed_args ) { if ( ! empty( $parsed_args['in_year'] ) ) { $year = absint( $parsed_args['in_year'] ); $where .= " AND YEAR(post_date) = $year"; } return $where; }, 10, 2 );
Then when you call
wp_get_archives()
, simply use thein_year
parameter to include only the archives for a specific year: (* You can change the parameter name, but also change it above.)$my_archives = wp_get_archives( array( 'type' => 'monthly', 'limit' => 10, 'echo' => 0, 'in_year' => '2018', // custom parameter ) );
But you can still use the standard
year
parameter to highlight/pre-select the relevant item.