1

I need to display custom posts archives on a page in the following format:

  • Monthly - Grouped by Month for the Current Year.
  • Annually - Grouped by Year & Month for all previous years

So I have got two functions to do so, which supposed to be called in a row:

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

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

    echo '<ul>';
        wp_get_archives( $previous_years_args );
    echo '</ul>';
}

Then I need to filter it so that the first function displays only archives for the current year and the second one does not display the current year.

The way this could have been done:

add_filter( 'getarchives_where', 'filter_monthly_archives', 10, 2 );

function filter_monthly_archives($text, $r) {
    return $text . " AND YEAR(post_date) = YEAR (CURRENT_DATE)";
}

And for the yearly archive we replace " AND YEAR(post_date) = YEAR (CURRENT_DATE)" with " AND YEAR(post_date) < YEAR (CURRENT_DATE)"

However, the filter applies globally and when I apply it, it affects both filters.

Is there a way of working around this (apply a specific filter for a specific wp_get_archives call) or another way to achieve the archives output as described above?

2

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;
}
  • It works like a charm, thanks. I never knew I can use a custom parameter. – Igor Skoldin Apr 8 '16 at 10:37
  • 1
    Adding your own custom parameter is always useful to trigger filters. Just remember to prefix them to avoid possible collision is future, like in this case, something like wpse_ – Pieter Goosen Apr 8 '16 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.