Skip to content

Instantly share code, notes, and snippets.

@smeyer
Created July 16, 2012 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smeyer/3119483 to your computer and use it in GitHub Desktop.
Save smeyer/3119483 to your computer and use it in GitHub Desktop.
attempt to save the same custom meta for blog, news and event posts, then sort a feed containing all of these by the meta
/*** Add Post Date Metabox */
function my_postdatemeta_init()
{
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_event_date_meta', 'ai1ec_event', 'normal', 'low');
}
add_action('admin_init','my_postdatemeta_init');
/*** 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);
/* Echo out the field */
echo '<p>For admin purposes. <strong>DO NOT USE</strong></p>';
echo '<input type="text" name="_post_date_meta" value="' . $env_post_date_meta . '" class="widefat" />';
}
/*** Save the Metabox Data */
function env_save_post_date_meta($post_id, $post) {
/* verify this came from the our screen and with proper authorization, */
/* because save_post can be triggered at other times */
if ( !wp_verify_nonce( $_POST['envmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
/* Is the user allowed to edit the post or page? */
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
/* OK, we're authenticated: we need to find and save the data */
/* We'll put it into an array to make it easier to loop though. */
$post_date_meta['_post_date_meta'] = $post->post_date;
/* Add values of $events_meta as custom fields */
foreach ($post_date_meta as $key => $value) { /* Cycle through the $home_meta array! */
if( $post->post_type == 'revision' ) return; /* Don't store custom data twice */
$value = implode(',', (array)$value); /* If $value is an array, make it a CSV (unlikely) */
if(get_post_meta($post->ID, $key, FALSE)) { /* If the custom field already has a value */
update_post_meta($post->ID, $key, $value);
} else { /* If the custom field doesn't have a value */
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); /* Delete if blank */
}
}
add_action('wp_insert_post', 'env_save_post_date_meta', 1, 2); // save the custom fields
/*** The Event Date Metabox */
function env_event_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);
/* Echo out the field */
echo '<p>For admin purposes. <strong>DO NOT USE</strong></p>';
echo '<input type="text" name="_post_date_meta" value="' . $env_post_date_meta . '" class="widefat" />';
}
/*** Save the Metabox Data */
function env_save_event_date_meta($post_id, $post) {
/* verify this came from the our screen and with proper authorization, */
/* because save_post can be triggered at other times */
if ( !wp_verify_nonce( $_POST['envmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
/* Is the user allowed to edit the post or page? */
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
/* OK, we're authenticated: we need to find and save the data */
/* We'll put it into an array to make it easier to loop though. */
global $wpdb;
$pID = $post->ID;
$event = $wpdb->get_var( $wpdb->prepare( "SELECT start FROM acM_ai1ec_events WHERE post_ID = '$pID'" ) );
$post_date_meta['_post_date_meta'] = $event;
/* Add values of $events_meta as custom fields */
foreach ($post_date_meta as $key => $value) { /* Cycle through the $home_meta array! */
if( $post->post_type == 'revision' ) return; /* Don't store custom data twice */
$value = implode(',', (array)$value); /* If $value is an array, make it a CSV (unlikely) */
if(get_post_meta($post->ID, $key, FALSE)) { /* If the custom field already has a value */
update_post_meta($post->ID, $key, $value);
} else { /* If the custom field doesn't have a value */
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); /* Delete if blank */
}
}
add_action('wp_insert_post', 'env_save_event_date_meta', 1, 2); // save the custom fields
/*** 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