Skip to main content
Added missing ;
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48
  1. 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>'select>';
    
  2. 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>'select>';
    
  3. 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>';
    
  1. 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;$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 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.

  1. 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>'
    
  2. 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>'
    
  3. 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>';
    
  1. 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 );
    
  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.

  1. 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>';
    
  2. 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>';
    
  3. 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>';
    
  1. 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 );
    
  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.

Better answer
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

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 monthlyfollowing 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 11Other 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.

  1. 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>'
    
  2. 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>'
    
  3. 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>';
    
  1. 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 );
    
  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.

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 = '<select>';
$my_archives .= wp_get_archives( array(
    'type'   => 'yearly', 
    'limit'  => 10,
    'echo'   => 0,
    'year'   => '2018',
    'format' => 'option', // this outputs an <option>
) );
$my_archives .= '</select>';

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 = '<select>';
$my_archives .= wp_get_archives( array(
    'type'     => 'monthly', 
    'limit'    => 10,
    'echo'     => 0,
    'year'     => '2018',
    'format'   => 'option',
    'monthnum' => 11,
) );
$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 (or November); then, you'd see the item/option being pre-selected.

  1. 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 );
    
  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.

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.

  1. 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>'
    
  2. 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>'
    
  3. 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>';
    
  1. 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 );
    
  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.

added 92 characters in body; edited body
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48
$my_archives = '<select>';
$my_archives .= wp_get_archives( array(
    'type'   => 'yearly', 
    'limit'  => 10,
    'echo'   => 0,
    'year'   => '2018',
    'format' => 'option', // this outputs an <option>
) );
$my_archives .= '</select>';
$my_archives = '<select>';
$my_archives .= wp_get_archives( array(
    'type'     => 'monthly', 
    'limit'    => 10,
    'echo'     => 0,
    'year'     => '2018',
    'format'   => 'option',
    'monthnum' => 11,
) );
$my_archives .= '</select>';
  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* You can change the parameter namename, if you wantbut 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 adduse aria-current="page" to highlight the item.

$my_archives = wp_get_archives( array(
    'type'   => 'yearly', 
    'limit'  => 10,
    'echo'   => 0,
    'year'   => '2018',
    'format' => 'option', // this outputs an <option>
) );
$my_archives = wp_get_archives( array(
    'type'     => 'monthly', 
    'limit'    => 10,
    'echo'     => 0,
    'year'     => '2018',
    'format'   => 'option',
    'monthnum' => 11,
) );
  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.

$my_archives = '<select>';
$my_archives .= wp_get_archives( array(
    'type'   => 'yearly', 
    'limit'  => 10,
    'echo'   => 0,
    'year'   => '2018',
    'format' => 'option', // this outputs an <option>
) );
$my_archives .= '</select>';
$my_archives = '<select>';
$my_archives .= wp_get_archives( array(
    'type'     => 'monthly', 
    'limit'    => 10,
    'echo'     => 0,
    'year'     => '2018',
    'format'   => 'option',
    'monthnum' => 11,
) );
$my_archives .= '</select>';
  1. 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 );
    
  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.

Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48
Loading