Depending on where you want your posts to show, the solution will vary. If you want to filter your main homepage, this solution will do:
/**
* Conditionally add a posts_where filter based on the current query
*
* @param object $query
* @return void
*/
function wpse_53599_24h_pre_get_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() )
return;
add_filter( 'posts_where', 'wpse_53599_filter_last_24h' );
}
add_action( 'pre_get_posts', 'wpse_53599_24h_pre_get_posts' );
/**
* Add a clause to posts_where to restrict posts to the last 24h
*
* @param string $where
* @return string
*/
function wpse_53599_filter_last_24h( $where = '' ) {
$where .= " AND post_date >= '" . date( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS ) . "'";
remove_filter( 'posts_where', 'wpse_53599_filter_last_24h' );
return $where;
}
Note that we remove the filter because we don't want to limit every posts query run on the site. Only our relevant ones.
If you want to filter a query other than the main home query, just change the conditional within the pre_get_posts
function.
If you want to filter a sub-query, it's even simpler, just add the posts_where
filter before your instantiation of WP_Query, and remove it immediately after, and skip the pre_get_posts
bit.