You ask a good question, where I would put it or how I would call it? I don\'t think you want to loop over all the posts everytime somebody visiting your site checking if any of them are more than one year old. Here is some thoughts about how this could be realized.
Assume you have a function to send a reminder about a post:
function send_reminder( $post_id ) {
if ( get_post_status( $post_id ) == \'publish\' ) {
$post_data = get_post( $post_id );
// send a reminder about the post
}
}
You can use a wp_schedule_single_event
function of WP Cron to schedule notification in a year when a new post gets published:
add_action(\'publish_post\', \'add_reminder\');
function add_reminder( $post_id ) {
wp_schedule_single_event( time() + 60*60*24*365, \'send_reminder\', array( $post_id ) );
}
However this action would run every time when a post gets any update. I don\'t know what is the best approach, read this Q/A for more information. Maybe using a transition_post_status
hook would be better.
And now you need to add such a scheduled action to every post already published on your site. You need to use custom WP_Query
loop over all the published posts and add a scheduled event for every one of them. This operation should be done only once, all new posts would have a reminder added via add_reminder
function. The best approach I could think of is to do all of this as a plugin and put this loop inside a function that would be triggered on a plugin activation.