WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm missing something somewhere, but I cannot connect the dots. Everything outputs properly except that the query returns everything from the DB, not just posts. Can anyone spot a reason for this behavior?

Having messed around a bit, I know it's related to add_filter, removing it allows the query to function normally. However, I still don't know what is causing the unintended behavior.

Solved: I left out a period...I forgot to concatenate the $where parameter passed into my_posts_where(). Gawd. I have been staring at this for 3 hours...

// Filter 'posts where' so we can limit the date range of a custom query
function my_posts_where( $where = '' ) {

// Posts from the last 30 days
// ERROR $where = " AND post_date > '" . date( 'Y-m-d', strtotime( '-30 days' ) ) . "'";
$where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-30 days' ) ) . "'";
return $where;

// Output a list of the most popular entries from the past 7 days
function my_popular_entries() {

// Filter the query
add_filter( 'posts_where', 'my_posts_where' );
$args = array(
    'posts_per_page' => 12,
    'orderby' => 'meta_value_num',
    'meta_key' => 'Views',
    'post_type' => 'post',
$query = new WP_Query( $args );
remove_filter( 'posts_where', 'my_posts_where' );

// Create the list, assuming we have posts
if ( $query->have_posts() ) :
    $html = '<section id="popular-entries" class="widget"><h1 class="widget-title">Popular Entries</h1><ul class="entries-list">';

    while ( $query->have_posts() ) : $query->the_post();

        $html .= '<li><article class="hentry">';
        $html .= '<div class="text"><h1 class="entry-title"><a href="' . get_permalink() . '" rel="bookmark">' . get_the_title() . '</a></h1></div>';
        $html .= '</article></li>';

    $html .= '</ul></section>';
    echo $html;

    // Reset the post variable
share|improve this question

closed as too localized by Rarst Aug 22 '12 at 12:42

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Please add your solution as an answer below and accept it. – mrwweb Jul 31 '12 at 17:50