How can I hide posts that are over 2 years old

You can use the pre_get_posts hook to modify the main query:

add_action( 'pre_get_posts', 'filter_old_posts' );
function filter_old_posts($query){
    if( !is_admin() && $query->is_main_query()){
         add_filter('posts_where', $callback = function( $where=""){
             $where .= " AND post_date > '" . date('Y-m-d', strtotime('-2 years')) . "'"; 
             return $where;
         });
         add_filter('getarchives_where', $callback );
    }
}

This will filter the main query posts to return posts newer than 2 years old.

There is also a second copy of the filter that uses getarchives_where to filter the archive widget results.

Leave a Comment