Skip to content

Instantly share code, notes, and snippets.

@lucasstark
Created September 11, 2013 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasstark/6524841 to your computer and use it in GitHub Desktop.
Save lucasstark/6524841 to your computer and use it in GitHub Desktop.
<?php
/*-----------------------------------------------------------------------------------*/
/* Custom Softwright Upcoming Events /*
/*-----------------------------------------------------------------------------------*/
if( ! class_exists( 'sw_upc_events_widget' ) ) :
class sw_upc_events_widget extends WP_Widget {
function sw_upc_events_widget() {
$widget_ops = array( 'description' => __( 'Displays closest Upcoming Events by date' ), 'classname' => 'upcoming-event-widget' );
$this->WP_Widget( 'upc_event_widget', __( 'Softwright Upcoming Events' ), $widget_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = ( $instance['title'] != '' ) ? esc_attr( $instance['title'] ) : __( 'Softwright Upcoming Events' );
if ( !$number = (int) $instance['number'] )
$number = 10;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;
$todaysDate = date('Ymd');
$args = array(
'post_type' => 'post' ,
'category_name' => 'seminars,trade-shows,webinars',
'posts_per_page' => $number,
'meta_query' => array(
'relation' => 'OR',
array('key' => 'start_date',
'value' => $todaysDate, // today's date
'compare' => '>='),
array('key' => 'end_date',
'value' => $todaysDate, // today's date
'compare' => '>=')
)
);
$include_posts = get_posts($args);
$ids = array();
foreach($include_posts as $p) {
$ids = $p->ID;
}
$args = array(
'post_type' => 'post' ,
'category_name' => 'seminars,trade-shows,webinars',
'posts_per_page' => $number,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts__in' => $ids
);
$myQuery = new WP_Query( $args );
// The Loop
if($myQuery->have_posts()) :
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php
while ( $myQuery->have_posts() ) : $myQuery->the_post();
$startdate = DateTime::createFromFormat('Ymd', get_field('start_date'));
$enddate = DateTime::createFromFormat('Ymd', get_field('end_date'));
$end = get_field('end_date');
$starttime = get_field('start_time');
$endtime = get_field('end_time');
?>
<li><strong><?php echo $startdate->format('m/d/Y'); ?><?php if($end) { ?> - <?php echo $enddate->format('m/d/Y'); } ?> <?php if($starttime) { ?>(<?php the_field('start_time'); ?> <?php if($endtime) { ?>to <?php the_field('end_time'); } ?>)<?php } ?> </strong><p class="excerpt"><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></p></li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php endif; ?>
<?php wp_reset_query(); // resets query so other calls are not affected
}
function update( $new_instance, $old_instance ){
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
return $instance;
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
if ( !isset($instance['number']) || !$number = (int) $instance['number'] )
$number = 5;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of Events to show'); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id('number') ); ?>" name="<?php echo esc_attr( $this->get_field_name('number') ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>" size="3" /></p>
<?php
}
}
endif;
register_widget('sw_upc_events_widget');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment