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?
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:
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 calladd_meta_box
where appropriate.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: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()
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: Toggle admin metabox based upon chosen page template
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:
You can change ‘2015-08-25’ to any value you want 😉