The year
parameter is, monthnum
, day
and w
(week) parameters are only used to highlight/pre-select the current item in the list/output and itthey only takestake effect on an archive page like date and category archives.
Try the following and visit any archive pages And by "highlight", and you'd see the yearI mean WordPress will add either 2018selected='selected'
option(if the format
is pre-selectedoption
) or aria-current="page"
(for other format
than link
) to the output. Something like this (note the selected
attribute):
$my_archives// =Assume '<select>';
$my_archives$results .=contains wp_get_archives(database's arrayrows (row objects).
// And '2018' is 'type'in the $args =>you 'yearly',pass to wp_get_archives().
$output = '';
foreach ( 'limit'$results as $row =>) 10,{
'echo'if ( is_archive() =>&& 0,
'2018' === $row->year ) 'year'{
=> '2018',
'format'$output =>.= 'option','<option /value="2018" selected>2018</option>';
this outputs an <option>
)} );else {
$my_archives $output .= '<'<option value="2018">2018</select>';option>';
}
}
If you changeTry the type
to monthly
following and specified a month (e.g.visit any archive pages, and you'd see the year 112018
for November) in addition tooption the year
parameter, like sois pre-selected:
$my_archives = '<select>';
$my_archives'<select>' .= wp_get_archives( array(
'type' => 'monthly''yearly',
'limit' => 10,
'echo' => 0,
'year' => '2018',
'format' => 'option',
// this 'monthnum'outputs =>an 11,<option>
) );
$my_archives .= '</select>';
Then if you are on an archive page and the current item in the list matches these: 1) The year is 2018
; and 2) The month is 11
Other examples below, assuming there's a (or Novemberpublic); then post on November 1, you'd see the item/option being pre-selected.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
is monthly
, 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
is weekly
, 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
is daily
, 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>';
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 the in_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 — and if the format
is not option
and not link
(which outputs a <link>
), WordPress will use aria-current="page"
to highlight the item.