WordPress.org

Ready to get started?Download WordPress

Forums

Get Archived Posts From Certain Month From All Years (2 posts)

  1. flyfisker
    Member
    Posted 7 months ago #

    I want to display an archive of all posts from each month regardless of the year. I know how to do this for a certain month from one year, but can't figure out how to do it for all years. I could create tags for each month and then tag the posts by the name of the month that were written in that month, but was hoping for an easier method.

    I then want to create a list of all of the months with links to these archives. So if you visited the archive for January, it would have every post written in January regardless of the year.

    There may be a simple way to do this that I am missing and I have searched the forums and Google and racked my brain, but can't figure it out.

  2. vtxyzzy
    Member
    Posted 7 months ago #

    You can pass the month value as an argument on your links, like this:

    <a href="http://mysite.com/page_for_archives/?month=02">February</a>

    Create a filter for the query in functions.php:

    function mam_posts_where ($where) {
       global $mam_global_where;
       if ($mam_global_where) $where .= " $mam_global_where";
       return $where;
    }
    add_filter('posts_where', 'mam_posts_where');

    And use a query like this in the template for the Page to get the posts:

    $month_to_show = $_GET['month'];
    $mam_global_where = " AND substr($wpdb->posts.post_date, 6, 2) = '$month_to_show' ";
    $my_posts = new WP_Query('posts_per_page=-1&ignore_sticky_posts=1');
    $mam_global_where = '';  // Turn off the filter
    if ($my_posts->have_posts()) {
       while ($my_posts->have_posts()) {
          $my_posts->the_post();
          echo "<p>DATE:$post->post_date MONTH:" . substr($post->post_date, 5,2) . "</p>";
       }
    }

Reply

You must log in to post.

About this Topic