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?

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;

If you really want to have only one post id and a value attached to it, well i think it will be better to store in options table using add_option and then using it using get_option..... that way you will be saved from headache of updating all posts meta at once when any of post is updated.....

  • +1 for add/update_option( 'featured_video', $post->ID );
    – kaiser
    Apr 5 '12 at 14:19
  • Thanks Rajeev. I ended up using update_option like you suggested. I added a custom meta box to the post type that, instead of saving meta details, updated a featured post option with the ID of that post.
    – kdev
    Apr 11 '12 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.