The 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
formatismonthly,November 2018would be pre-selected:$my_archives="<select>" . wp_get_archives( array( 'type' => 'monthly', 'limit' => 10, 'echo' => 0, 'year' => '2018', 'format' => 'option', 'monthnum' => 11, ) ) . '</select>'; -
When
formatisweekly,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
formatisdaily,November 1, 2018would 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.phpfile: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_yearparameter 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
yearparameter to highlight/pre-select the relevant item.