Make use of custom parameter, lets call it wpse__current_year
, which will accept two values, true
(includes current year) and false
(excludes current year). Lets incorporate that
function show_monthly_archive( $post_type )
{
$current_year_args = array(
'type' => 'monthly',
'limit' => '12',
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC',
'post_type' => $post_type,
'wpse__current_year' => true
);
echo '<ul>';
wp_get_archives( $current_year_args );
echo '</ul>';
}
function show_yearly_archive( $post_type )
{
$previous_years_args = array(
'type' => 'yearly',
'limit' => '',
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC',
'post_type' => $post_type,
'wpse__current_year' => false
);
echo '<ul>';
wp_get_archives( $previous_years_args );
echo '</ul>';
}
We can now modify our filter accordingly
add_filter( 'getarchives_where', 'filter_monthly_archives', 10, 2 );
function filter_monthly_archives( $text, $r )
{
// Check if our custom parameter is set, if not, bail early
if ( !isset( $r['wpse__current_year'] ) )
return $text;
// If wpse__current_year is set to true
if ( true === $r['wpse__current_year'] )
return $text . " AND YEAR(post_date) = YEAR (CURRENT_DATE)";
// If wpse__current_year is set to false
if ( false === $r['wpse__current_year'] )
return $text . " AND YEAR(post_date) < YEAR (CURRENT_DATE)";
return $text;
}