Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created May 21, 2012 15:23
Show Gist options
  • Save lgedeon/2762896 to your computer and use it in GitHub Desktop.
Save lgedeon/2762896 to your computer and use it in GitHub Desktop.
<?php
public function event_content_relationship_meta_box( $post ) {
wp_nonce_field( 'tc_event_relationship_noncename', 'tc_event_relationship_nonce' );
$current_term = array();
if ( $current_relationship = get_post_meta( $post->ID, '_tc_event_relationship', true ) ) {
if ( $event_type = get_the_terms( (int) $current_relationship, 'tc_event_type_taxonomy' ) )
$current_term = get_term( $event_type[0]->term_id, 'tc_event_type_taxonomy' );
}
$eventtypes = get_terms( array( 'tc_event_type_taxonomy' ) );
?>
<script>
//Event Type autocomplete
var tc_eventTypes = [
<?php
foreach ( $eventtypes as $type ) : ?>
{ label: "<?php echo esc_js( $type->name ); ?>", value: "<?php echo esc_js( $type->name ); ?>", termid: "<?php echo esc_js( $type->term_id ); ?>" },
<?php endforeach; ?>
];
jQuery(document).ready(function() {
//Add autocomplete with functionon select to grab the appropriate event posts.
jQuery( '#event-type-name' ).autocomplete({
source: tc_eventTypes,
html: true,
select: function(event, ui) {
jQuery('#event-type-name').val(ui.item.label);
jQuery('#tc-event-relationship-container').html('<img src="<?php echo admin_url( '/images/loading.gif' ); ?>" alt="Loading..." />');
jQuery.post( ajaxurl, {
action: 'tc_load_events_of_type',
event_type_term_id: ui.item.termid,
}, function(response){
if( '' == response ) {
jQuery('#tc-event-relationship-container').html('<p>There are no recently ended or close approaching events of this type.</p>');
} else {
jQuery('#tc-event-relationship-container').html(response);
}
});
return false;
}
});
});
</script>
<p>
<label for="event-type-name" class="screen-reader-text">Event Type:</label>
<input type="text" placeholder="Event Type" class="widefat" id="event-type-name" name="event-type-name" value="<?php echo ( empty( $current_term ) ) ? '' : $current_term->name; ?>" />
<span class="description">Type the event type.</span>
</p>
<div id="tc-event-relationship-container">
<?php
if ( ! empty( $current_term ) ) {
?>
<select id="_tc_event_relationship" name="_tc_event_relationship" class="widefat">
<option></option>
<?php
$events = get_posts(
array(
'post_type' => 'tc_events',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => '_tc_event_start',
'posts_per_page' => 100,
'meta_query' => array(
array(
'key' => '_tc_event_start',
'value' => get_date_from_gmt( date( 'Y-m-d H:i:s' ), 'U' ) + 5184000, //let's say event start is up to 2 months in the future
'compare' => '<=',
'type' => 'NUMERIC',
),
array(
'key' => '_tc_event_end',
'value' => get_date_from_gmt( date( 'Y-m-d H:i:s' ), 'U' ) - 31104000, //and lets say all events that ended 12 months prior
'compare' => '>=',
'type' => 'NUMERIC',
),
),
'tax_query' => array(
array(
'taxonomy' => 'tc_event_type_taxonomy',
'field' => 'id',
'terms' => $current_term->term_id,
),
),
)
);
if ( $events ) {
foreach ( $events as $event ) {
?>
<option id="event-<?php echo $event->ID; ?>" value="<?php echo $event->ID; ?>" <?php selected( $event->ID, (int) $current_relationship ); ?>><?php echo get_the_title( $event->ID ); ?></option>
<?php
}
}
?>
</select>
<?php
}
?>
</select>
</div><!-- end tc-event-relationship-container -->
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment