Something I just learned and thought I’d share to save others the trouble: if you’re attempting to filter the results of get_posts()
, for example with the posts_where
filter, you need to disable suppress_filters
.
For example, here’s how to fetch some posts from within the past 30 days (based on an example from the Codex):
<?php function last_thirty_days( $where = '' ) { global $wpdb; $where .= $wpdb->prepare( " AND post_date > %s", date( 'Y-m-d', strtotime('-30 days') ) ); return $where; } add_filter( 'posts_where', 'last_thirty_days' ); $some_posts = get_posts( array( 'suppress_filters' => false ) ); // Important to avoid modifying other queries remove_filter( 'posts_where', 'last_thirty_days' ); ?>