Skip to main content
1 of 4
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

The year parameter is only used to highlight the current item in the list/output and it only takes effect on an archive page like date and category archives.

Try the following and visit any archive pages, and you'd see the year 2018 option is pre-selected:

$my_archives = wp_get_archives( array(
    'type'   => 'yearly', 
    'limit'  => 10,
    'echo'   => 0,
    'year'   => '2018',
    'format' => 'option', // this outputs an <option>
) );

If you change the type to monthly and specified a month (e.g. 11 for November) in addition to the year parameter, like so:

$my_archives = wp_get_archives( array(
    'type'     => 'monthly', 
    'limit'    => 10,
    'echo'     => 0,
    'year'     => '2018',
    'format'   => 'option',
    'monthnum' => 11,
) );

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 (or November); then, you'd see the item/option being pre-selected.

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:

  1. This should go in the theme's functions.php file:

    add_filter( 'getarchives_where', function ( $where, $parsed_args ) {
        if ( 'monthly' === $parsed_args['type'] &&
            ! empty( $parsed_args['in_year'] )
        ) {
            $year = absint( $parsed_args['in_year'] );
            $where .= " AND YEAR(post_date) = " . $year;
        }
        return $where;
    }, 10, 2 );
    
  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 the parameter name, if you want)

    $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 add aria-current="page" to highlight the item.

Sally CJ
  • 39.9k
  • 2
  • 26
  • 48