Title: posts_where filter doesn&#039;t work in a class.
Last modified: August 21, 2016

---

# posts_where filter doesn't work in a class.

 *  Resolved [gpspake](https://wordpress.org/support/users/gpspake/)
 * (@gpspake)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/posts_where-filter-doesnt-work-in-a-class/)
 * I’m trying to get all of the posts with a custom post type that have been published
   in the past 5 days.
 * Although this is overly difficult, and it doesn’t look like [a better method will be available until 3.7](http://www.viper007bond.com/2013/08/27/date-queries-in-wordpress-3-point-7/),
   I’ve managed to get it working inside of a custom template by filtering adding
   a filter to posts_where.
 * The problem is, I need to do this inside of a class and I can’t figure out what
   I’m doing wrong. I’m almost certain that it is my code that is the problem.
 * Here is the code that works:
 *     ```
       function filter_where( $where = '' ) {    // posts in the last 5 days
         $where .= " AND post_date > '" . date('Y-m-d', strtotime('-5 days')) . "'";
         return $where;
       }
   
       add_filter( 'posts_where', 'filter_where' );
   
       $args = array(
        'post_type' => 'listserv',
        'tax_query' => array(
          'relation' => 'AND',
          array(
            'taxonomy' => 'listserv',
            'field' => 'slug',
            'terms' => 'staff-listserv'
         )
        )
       );
   
       $query = new WP_Query( $args );
       echo '<div id="content" style="">';
       while( $query->have_posts() ) : $query->the_post();
   
       echo '<a href="#">'. get_the_title() .'</a><br/>';
   
       endwhile;
       echo '</div>';
   
       remove_filter( 'posts_where', 'filter_where' );
       ?>
       ```
   
 * The problem is, when I put everything in to a class and move the second part 
   in to a method, it simply returns the 8 most recently published posts and pages.
 *     ```
       Here's what that code looks like:
   
       class Test_Range {
   
       	private function filter_where( $where = '' ) {    // posts in the last 30 days
       	    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-40 days')) . "'";
       	    return $where;
       	}
   
       	public function range_announcements() {
   
       		$filter_where = $this->filter_where();
   
       		add_filter( 'posts_where', $filter_where);
   
       			$args = array(
       							'post_type' => 'listserv',
       							'suppress_filters' => false,
       							'tax_query' => array(
       								'relation' => 'AND',
       								array(
       									'taxonomy' => 'listserv',
       									'field' => 'slug',
       									'terms' => 'staff-listserv'
       								)
       							)
       						);
   
       		$query = new WP_Query( $args );
       		$announcements = '<div id="content" style="">';
       		while( $query->have_posts() ) : $query->the_post();
   
           $announcements .= '<a href="#">'. get_the_title() .'</a><br/>';
   
           endwhile;
           $announcements .= '</div>';
   
       		echo $announcements;
   
       		remove_filter( 'posts_where', $filter_where );
       	}
   
       }
   
       $testing_the_range = New Test_Range();
       $testing_the_range -> range_announcements();
       ```
   
 * I’ve been messing with this for hours and I’m not sure what to try next. Any 
   suggestions?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [gpspake](https://wordpress.org/support/users/gpspake/)
 * (@gpspake)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/posts_where-filter-doesnt-work-in-a-class/#post-4113575)
 * Well, duh.
    I made my filter_where method public and it works now. Thank goodness.
   [Fortunately this will much easier in 3.7](http://www.viper007bond.com/2013/08/27/date-queries-in-wordpress-3-point-7/)
 *  [viper007bond](https://wordpress.org/support/users/viper007bond/)
 * (@viper007bond)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/posts_where-filter-doesnt-work-in-a-class/#post-4113608)
 * Yes, the callback function needs to be publicly callable and also instead of 
   assigning the callback function to a variable, you can just add the filter like
   this:
 * `add_filter( 'posts_where', array( $this, 'filter_where' ) );`
 *  Thread Starter [gpspake](https://wordpress.org/support/users/gpspake/)
 * (@gpspake)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/posts_where-filter-doesnt-work-in-a-class/#post-4113610)
 * Cool. I dropped the ampersand. All good.
    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘posts_where filter doesn't work in a class.’ is closed to new replies.

## Tags

 * [methods](https://wordpress.org/support/topic-tag/methods/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [gpspake](https://wordpress.org/support/users/gpspake/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/posts_where-filter-doesnt-work-in-a-class/#post-4113610)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
