'Wordpress - Display Posts Newer Than 30 Days

I'm going nuts trying to figure out why I'm having such a difficult time getting WordPress to only show posts newer than 30-days and I could really use a second set of eyes. I'm using the following code but nothing is showing up on my site.

<?php
// Create a new filtering function that will add our where clause to the query
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' );
$the_query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
?>


<?php if ($the_query->have_posts()) : ?>

    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>


        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h2 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <div class="meta">
             <span class="date">
                <strong><?php the_time('d'); ?></strong>
                <strong><?php the_time('M'); ?></strong>
             </span>


            </div>

            <div class="entry">
            <?php if ( function_exists( 'get_the_image' ) ) {
        get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'alignleft', 'width' => '170', 'height' => '155' ) ); }
        ?>

                <?php the_content('Read More'); ?>
            </div>


        </div>

    <?php endwhile; ?>

    <div class="navigation">
    <?php
        include('includes/wp-pagenavi.php');
        if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
    ?>
    </div>

<?php else : ?>

    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php get_search_form(); ?>

<?php endif; ?>


Solution 1:[1]

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

This works

Solution 2:[2]

The way you're doing looks fine. Try putting posts_per_page => -1 as the argument.

The following works for me:

function filter_where( $where = '' ) {
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$args = array(
    'posts_per_page' => -1,
);
$the_query = new WP_Query($args);
remove_filter( 'posts_where', 'filter_where' );

while ($the_query->have_posts()) {
    $the_query->the_post();
    // do stuff
}

Hope it helps.

Solution 3:[3]

I know this is super old but, you can do this with WP_Query now.

$args = array(
  'post_type' => 'post', // or whatever post type you want
  'posts_per_page' => -1, // get all posts that apply, i.e. no limit
  'date_query' => array(
     array(
       'after' => '-30 days',
       'column' => 'post_date',
     ),
  ),
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 D-man
Solution 2 vmassuchetto
Solution 3 Nathan