Not sure about the linked class – it seems they collect metaboxes immediately and so there is no information regarding what post is being viewed.
In general though – yes it is possible. To add a metabox:
add_action( 'add_meta_boxes', 'myplugin_add_my_custom_box',10,2);
See the source code here. This add_meta_boxes
hook passes two variables: the post type and the post object. You can use the post to get the post meta and then call add_meta_box
where appropriate.
function myplugin_add_my_custom_box($post_type,$post){
//Get event date as a timestamp
$event_date = (int) get_post_meta($post->ID,'_wr_event_date',true);
//Check date exists and is in the past. If not, return: don't add metabox.
if( empty($event_date) || current_time('timestamp') < $event_date)
return;
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_metabox_callback',
'event'
);
}
You’ll notice there is also a add_meta_boxes_{$post_type}
hook – this is more efficient if you want to just add it to ‘event_cpt’ post types:
add_action( 'add_meta_boxes_event_cpt', 'myplugin_add_my_custom_box',10,1);
In this case your callback only includes the $post
as an argument!
Note: Avoid using php date/time functions: this will, by default, always have the timezone set to UTC. If you want the current date/time in the blogs timezone using something like current_time()