pre_get_posts 5
<?php
// Load our function when hook is set
add_action( 'pre_get_posts', 'rc_modify_query_get_posts_by_date' );
// Modify the current query
function rc_modify_query_get_posts_older_than_today( $query ) {
// Check if on frontend and main query is modified
if( ! is_admin() && $query->is_main_query() ) {
$query->set( 'order', 'ASC' );
add_filter( 'posts_where', 'rc_filter_where' );
}
return $query;
}
// Filter posts older than today
function rc_filter_where( $where = '' ) {
$today = date( 'Y-m-d' );
$where .= " AND post_date >= '$today'";
return $where;
}