Only show metabox when date-value in other metabox is over?

probably a weird question.
I’m using the custom MetaBox and CustomFields Class by jaredatch on Github.

I have this “event-date” metabox:

Read More
$meta_boxes[] = array(
        'id'         => 'event_date',
        'title'      => 'Event Date',
        'pages'      => array( 'wr_event', ),
        'context'    => 'normal',
        'priority'   => 'high',
        'fields'     => array(
            array(
                'name' => 'Test Date Picker (UNIX timestamp)',
                'desc' => 'field description (optional)',
                'id'   => $prefix . 'event_date',
                'type' => 'text_date_timestamp',
            )
        ),
    );

I have a second metabox called “event-review”

$meta_boxes[] = array(
    'id'         => 'wr_event_review',
    'title'      => 'Event Review',
    'pages'      => array( 'wr_event', ), // Post type
    'context'    => 'normal',
    'priority'   => 'high',
    'show_names' => true, // Show field names on the left
    'fields'     => array(
        array(
            'name'    => 'Event Review',
            'id'      => $prefix . 'event_wysiwyg',
            'type'    => 'wysiwyg',
            'options' => array( 'textarea_rows' => 5, ),
        )
    ),
);

I wonder if it’s possible to show the event-review metabox only when the date is over?

Something like…

if ( date('U') > date('U', $_POST["_wr_event_date"] ) ) {
     $meta_boxes[] = array(
        'id'         => 'wr_event_review',
        'title'      => 'Event Review',

However I have no idea if this is even possible or even how I can get the current event_date that is in the input.

Any thoughts on this?

Related posts

Leave a Reply

3 comments

  1. 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()

  2. If you wanna show or hide entire meta box when event date is change, you can try to use Conditional Logic plugin, so you can rewrite your code like so:

        $meta_boxes[] = array(
        'id'         => 'wr_event_review',
        'title'      => 'Event Review',
        'pages'      => array( 'wr_event', ), // Post type
        'context'    => 'normal',
        'priority'   => 'high',
        'show_names' => true, // Show field names on the left
        'visible'    => array('event_date', '>', '2015-08-25'),
        'fields'     => array(
            array(
                'name'    => 'Event Review',
                'id'      => $prefix . 'event_wysiwyg',
                'type'    => 'wysiwyg',
                'options' => array( 'textarea_rows' => 5, ),
            )
        ),
    );
    

    You can change ‘2015-08-25’ to any value you want 😉