Use the date_query
parameter in a query1. Make sure you query posts that are both the age you require and currently public.
Set the post status to private2 with the wp_insert_post()
3 function.
Here’s an example from the codex which should give you an idea for how to query for posts older than a certain date:1
Return posts made over a year ago but modified in the past month
$args = array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'before' => '1 year ago',
),
array(
'column' => 'post_modified_gmt',
'after' => '1 month ago',
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
Additionally, you can add the wp_safe_redirect()
4 function to private pages to redirect them elsewhere if the post is private. You can create a condition similar to this5 in a page template, for example, to check the status:
if ( get_post_status ( $ID ) == 'private' ) {
echo 'private';
} else {
echo 'public';
}