WordPress.org

Ready to get started?Download WordPress

Forums

How do you show category posts ONLY from last 2 years? (3 posts)

  1. gbengston
    Member
    Posted 4 years ago #

    I was wondering if someone can help me with this. I am running multiple loops on a client's news page and they came back to me saying that they want the loops to only show the past 2 years of posts with the rest being viewable via the archive links.

    This is one of the loops on the news page:

    <!-- Start the Loop for News. -->
    <h3>Corporate News</h3>
    <?php $my_query = new WP_Query('category_name=corporate&posts_per_page=5');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <br />
    <div class="morenews"><a href="<?php bloginfo('home'); ?>/news/corporate/">More from Corporate »</a></div>
    <!-- End the Loop for Corporate News. -->

    As you can see, I'm using "WP_Query" in the loop, and calling the category via "Category_Name". Can someone please show me how to turn what I have into a loop that will limit the posts to ones posted in the past 2 years? Any help would be greatly appreciated.

    Thanks in advance,
    Greg

  2. Jonas Grumby
    Member
    Posted 4 years ago #

  3. gbengston
    Member
    Posted 4 years ago #

    Yeah, I saw that but I couldn't get it to work.

    I'm assuming you're referring to this:

    <?php
    //based on Austin Matzko's code from wp-hackers email list
      function filter_where($where = '') {
        //posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>

    Any idea on how to combine what I have with the Matzko example?

Topic Closed

This topic has been closed to new replies.

About this Topic