Skip to content

Instantly share code, notes, and snippets.

@codehooligans
Created July 16, 2012 19:32
Show Gist options
  • Save codehooligans/3124544 to your computer and use it in GitHub Desktop.
Save codehooligans/3124544 to your computer and use it in GitHub Desktop.
Gist for Stephanie
<?php
add_action('admin_init','my_postdatemeta_init');
add_action('save_post', 'env_save_post_date_meta', 1, 2); // save the custom fields
/*** Add Post Date Metabox */
function my_postdatemeta_init()
{
// Paul: The metaboarcx are really just display for the admin to check the date. Should not be seen for regular users.
if (get_current_user_id() == 1) { // Change the '1' to some other userID
// Paul: changed so all post_types use the same metabox. Don't need one just for Events.
add_meta_box('env_post_date_meta', 'Admin Meta', 'env_post_date_meta', 'post', 'normal', 'low');
add_meta_box('env_post_date_meta', 'Admin Meta', 'env_post_date_meta', 'news', 'normal', 'low');
add_meta_box('env_event_date_meta', 'Admin Meta', 'env_post_date_meta', 'ai1ec_event', 'normal', 'low');
}
}
/*** The Post Date Metabox */
function env_post_date_meta() {
global $post;
/* Noncename needed to verify where the data originated */
echo '<input type="hidden" name="envmeta_noncename" id="envmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
/* Get the location data if its already been entered */
$env_post_date_meta = get_post_meta($post->ID, '_post_date_meta', true);
if (!$env_post_date_meta) {
$env_post_date_meta = '';
}
/* Echo out the field */
echo '<p>For admin purposes. <strong>DO NOT USE</strong></p>';
echo '<input disabled="disabled" type="text" name="_post_date_meta" value="' . $env_post_date_meta . '" class="widefat" />';
// as a coutesy we display the formmated date below the input field
if (intval($env_post_date_meta)) {
$format = get_option('date_format') .' '. get_option('time_format');
$display_date = gmdate($format, $env_post_date_meta);
echo '<p class="description">'. $display_date .'</p>';
}
}
/*** Save the Metabox Data */
function env_save_post_date_meta($post_id, $post) {
if (!$post_id)
return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( wp_is_post_revision( $post_id ) )
return;
/* Is the user allowed to edit the post or page? */
if ( !current_user_can( 'edit_post', $post_id ))
return;
// If the user has not POSTed the form yet then ignore for now.
if (!isset($_POST['post_type']))
return;
// IF the post_type is an event (ai1ec_event) we gather the event date...
if (esc_attr($_POST['post_type']) == 'ai1ec_event') {
if (isset($_POST['ai1ec_start_time'])) {
// The 'ai1ec_start_time' field is a UNIX timestamp. This is most accurate. So store as is.
update_post_meta($post->ID, '_post_date_meta', intval($_POST['ai1ec_start_time']));
}
} else if ((esc_attr($_POST['post_type']) == 'post') || (esc_attr($_POST['post_type']) == 'news')) {
// ..for other post_types we simply use the publish date.
// For other post_types we use the $post->post_date field but convert it to a UNIX timestamp
if (isset($post->post_date)) {
$post_date = strtotime($post->post_date);
update_post_meta($post->ID, '_post_date_meta', $post_date);
}
}
}
/*** Function for displaying featured posts from multiple custom types in a short feed, no pagination */
function env_display_feed_from_types($the_type, $the_order, $the_number = 3, $orderby = 'date', $status = 'publish')
{
$args = array(
'post_type' => ($the_type),
'posts_per_page' => $the_number,
'order' => $the_order,
'post_status' => ($status),
'meta_query' => array(
array(
'key' => '_home_feat',
'value' => 'on'
),
array(
'key' => '_post_date_meta'
)
),
'meta_key' => '_post_date_meta',
'orderby' => 'meta_value_num'
);
$my_posts = new WP_Query( $args );
if( $my_posts->have_posts() ):
echo '<ul class="short_feed">';
while ( $my_posts->have_posts() ) : $my_posts->the_post();
global $post;
if (get_post_type($post->ID) == "ai1ec_event") { $ptype = "event"; $event = Ai1ec_Events_Helper::get_event($post->ID); } else if (get_post_type($post->ID) == "news") { $ptype = "news"; } else { $ptype = "blog"; } ?>
<li class="post_items clear">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/feat_<?php echo $ptype; ?>.jpg" alt="<?php echo $ptype; ?>" />
<div class="p_data">
<a href="<?php the_permalink() ?>" class="p_title"><span><?php the_title(); ?> |</span> <?php if ($ptype == "event") { echo $event->long_start_date; } else { the_time('m.d.y'); } ?></a>
<?php the_excerpt(); ?>
</div>
</li><!--post_items-->
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_postdata();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment