Editing your code to use a custom field is fairly trivial…
function close_comments( $posts ) {
if ( !is_single() ) {
return $posts;
}
$cmeta = get_post_meta($posts[0]->ID,'your_field_name',true);
if ( $posts[0]->post_date_gmt < $cmeta ) {
$posts[0]->comment_status="closed";
$posts[0]->ping_status="closed";
}
return $posts;
}
add_filter( 'the_posts', 'close_comments' );
I am not sure that that approach is the right one though.
- You are really just brute forcing a closed status. This is a
“display only” hack. It doesn’t change the database status at all. - This runs on the front end, every time any post displays and runs for many other queries as well. That strikes me as pretty profligate.
I think I would rethink the whole thing to use wp_cron