您可以在查询中设置自己的自定义查询变量,然后使用该值设置稍后在过滤器中读取的类变量:
class WPA69844_where {
var $days;
public function __construct(){
add_action( \'parse_query\', array( $this, \'parse_query\' ) );
}
public function parse_query( $query ) {
if( isset( $query->query_vars[\'my_days_var\'] ) ):
$this->days = $query->query_vars[\'my_days_var\'];
add_filter( \'posts_where\', array( $this, \'filter_where\' ) );
add_filter( \'posts_selection\', array( $this, \'remove_where\' ) );
endif;
}
public function filter_where($where = \'\') {
$where .= " AND post_date > \'" . date(\'Y-m-d\', strtotime(\'-\' . $this->days . \' days\')) . "\'";
return $where;
}
public function remove_where() {
remove_filter( \'posts_where\', array( $this, \'filter_where\' ) );
}
}
$wpa69844_where = new WPA69844_where();
那么对于您的查询:
$args = array( \'my_days_var\' => 30 );
$posts = new WP_query( $args );