I have a metabox on my post type of 'ebrd_videos' that sets the video as a 'featured video' using a checkbox.
I would like to limit the number of videos with the status of 'featured = on' to 1. So that when I set a video as featured, and publish/update that video, all other videos are set to 'featured = off'.
If it helps, here is the code to create the meta box:
// Add the Top and Featured News Meta Boxes ----------------------------------------//
add_action( 'add_meta_boxes', 'ebrd_add_post_metaboxes' );
function ebrd_add_post_metaboxes() {
add_meta_box('ebrd_feat', 'Post status', 'ebrd_feat', 'ebrd_videos', 'side', 'default');
}
// Featured Metabox
function ebrd_feat(){
global $post;
$custom = get_post_custom($post->ID);
if(isset($custom["ebrd_home_feat"][0])) $ebrd_home_feat = $custom["ebrd_home_feat"][0];
else $ebrd_home_feat = 'off';
// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<input type="checkbox" name="ebrd_home_feat" <?php if( $ebrd_home_feat != 'off' ) { ?>checked="checked"<?php } ?> /> Home feature?
<?php
}
add_action('save_post', 'save_details');
function save_details($post_ID = 0) {
$post_ID = (int) $post_ID;
$post_type = get_post_type( $post_ID );
$post_status = get_post_status( $post_ID );
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
if(isset($_POST["ebrd_home_feat"])) $ebrd_home_feat = $_POST["ebrd_home_feat"];
else $ebrd_home_feat = 'off';
if ($post_type) {
update_post_meta($post_ID, "ebrd_home_feat", $ebrd_home_feat);
}
return $post_ID;
}