Skip to main content
2 of 2
added 623 characters in body
Milo
  • 78.8k
  • 4
  • 127
  • 168

You can get the queried date from the query vars in the $wp_query object:

if( is_date() ){
    if( isset( $wp_query->query_vars['year'] ) ){
        echo 'queried year is ' . $wp_query->query_vars['year'];
    }
    if( isset( $wp_query->query_vars['monthnum'] ) ){
        echo 'queried month is ' . $wp_query->query_vars['monthnum'];
    }
    if( isset( $wp_query->query_vars['day'] ) ){
        echo 'queried day is ' . $wp_query->query_vars['day'];
    }
}

or just via get_query_var():

echo get_query_var( 'year' );

Does that answer your question?

EDIT

You can check if the site is using pretty permalinks by checking for the presence of the permalink structure option. In that case, you can fetch and parse the m query var instead of year / monthnum / day:

if( get_option('permalink_structure') ){
    // using pretty permalinks
} else {
    // not using pretty permalinks
    // note that month or day may be empty
    // test for their existence before trying to use!
    $year = substr( get_query_var( 'm' ), 0, 4);
    $month = substr( get_query_var( 'm' ), 4, 2);
    $day = substr( get_query_var( 'm' ), 6, 2);
}
Milo
  • 78.8k
  • 4
  • 127
  • 168