Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

I have this "event-date" metabox:

$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?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
add comment

I'd go for a JavaScript solution here. That way you have to possibility to instantly show or hide the event review metabox based on the date entered in the event date metabox.

I provided a solution for a very similar problem in which a metabox needed to be toggled based upon the chosen page template.

Please, see: Admin Conditional - If template is assigned

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.