[resolved] Limit wp_get_archives to show months for the X years only (8 posts)

  1. clubfredd
    Posted 4 years ago #


    Can anybody show me how to set a limit on wp_get_archives to show archives by month BUT only those for the year 2010 and above - so the 2009 monthly archives are not shown on the sidebar archive widget.

    Thank you!

  2. You could try limiting the display to something like the last 2 years (24 months) - not exactly what you want, but perhaps an acceptable compromise?

    <?php wp_get_archives('type=monthly&limit=24'); ?>

    Tweaked from example in the Codex here:

  3. clubfredd
    Posted 4 years ago #

    Hello Zoonini, thank you for the reply.
    The problem is that there are months that I don't have posts (e.g. 7 out of 12 months for 2010 and 7 months for 2011). And if I set the limit to, let's say 14, the fix is only applicable now but when posts come in for the months to come, there will be months from 2010 that will disappear.

    I don't know php, but is it possible to grab that year element in output of type=monthly and apply a conditional wherein if the value of that year element is greater than 2009 to display it and if it's less than or equal to 2009 to not display it?

  4. Unfortunately I'm not enough of a programmer to answer this for you - hopefully someone else will chime in!

  5. Ron Rennick
    MultiSite Guru
    Posted 4 years ago #

    You could use the getarchives_where filter and add a condition like

    AND (year > 2009 OR (year = 2009 AND month >= 11))

    Then you just need to calculate the 2009 & 11 using php date functions.

  6. Jeremy Clarke
    Posted 4 years ago #

    This is a pretty specific need and ultimately not something that makes a lot of sense. While I can imagine strange reasons why you'd want to hide 2009, I'd personally recommend re-evaluating the need for this hack instead. Does hiding 2009 really make sense? What's the content in there that's so important to hide? Why not either remove that content or allow the months to be present?

    To achieve what you want you're going to need some PHP. I think the simplest method would be to use the WHERE filter on the SQL used by wp_get_archives. Here's what the filter looks like in the source:

    $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );

    What we need to do is add another AND clause that will exclude posts from before 2010. I just worked out the code. It's not particularly well tested but it works in the twenty-eleven theme and doesn't break other uses of wp_get_archives(), which should behave normally:

    BACKUP COPY OF CODE: http://pastie.org/2806085

    [Code moderated as per the Forum Rules. Please use the pastebin]

    To use this you'd need to put the stuff above into your functions.php file or a new plugin, then use the jer_wp_get_archives_pre_2009 in your theme instead of wp_get_archives(). As the phpdoc says, jer_wp_get_archives_pre_2009() takes the same arguments as wp_get_archives(), so you can add any other args there.

    Like I said though, seriously consider changing your requirements rather than implementing this, it's an awkward hack that might break in the future.

  7. clubfredd
    Posted 4 years ago #

    Thank you so much for all your replies. Especially jeremyclarke for the foundation. Extremely helpful indeed!

    I just designed a theme for a client and hiding the 2009 archive was one of the last things he wanted done so I don't know for what purpose.

    Thanks so much everyone!

Topic Closed

This topic has been closed to new replies.

About this Topic