How do I create a featured post within a custom post type?

Do I do this exactly the same way as I would with normal posts? I use a $featured_cat global variable and pull that out. Should I do the same here?

Or should I use an associated custom taxonomy? Or perhaps I am over-thinking it? Would there really be a benefit with taxonomies?

Read More

What’s the right approach here, guys?

Related posts

Leave a Reply

2 comments

  1. Register a video post type and a “Featured Taxonomy” “Featured Custom Meta Select Box

    The Post type:

    function c3m_reg_vid_post() {
                
                  $labels = array(
                    'name' => _x('Videos', 'post type general name'),
                    'singular_name' => _x('Video', 'post type singular name'),
                    'add_new' => _x('Add New', 'video'),
                    'add_new_item' => __('Add New Video'),
                    'edit_item' => __('Edit Video'),
                    'new_item' => __('New Video'),
                    'view_item' => __('View Video'),
                    'search_items' => __('Search Videos'),
                    'not_found' =>  __('No videos found'),
                    'not_found_in_trash' => __('No videos found in Trash'), 
                    'parent_item_colon' => ''
                  );
                  $args = array(
                    'labels' => $labels,
                    'public' => true,
                    'publicly_queryable' => true,
                    'show_ui' => true, 
                    'query_var' => true,
                    'show_in_nav_menus' => true,
                    'can_export' => true,
                    'rewrite' => array('slug' => 'video', 'with_front' => false),
                    'capability_type' => 'post',
                    'register_meta_box_cb' => 'c3m_video_meta', //This is for our custom meta box
                    'hierarchical' => false,
                    'menu_position' => 10,
                   'taxonomies' => array('featured'),
                    'supports' => array('title', 'editor' 'custom-fields')
                  ); 
                  register_post_type('video', $args );
    }
    

    ###The Taxonomy

    On second thought lets use a custom field for the featured video post and create a drop down select custom meta box to choose if the post is featured.

    Set up the custom meta box:

    //hook to add a meta box
    add_action( 'add_meta_boxes', 'c3m_video_meta' );
    
    function c3m_video_meta() {
        //create a custom meta box
        add_meta_box( 'c3m-meta', 'Featured Video Selector', 'c3m_mbe_function', 'video', 'normal', 'high' );
    }
    
    function c3m_mbe_function( $post ) {
    
        //retrieve the meta data values if they exist
        $c3m_mbe_featured = get_post_meta( $post->ID, '_c3m_mbe_featured', true );
    
        echo 'Select yes below to make video featured';
        ?>
        <p>Featured: 
        <select name="c3m_mbe_featured">
            <option value="No" <?php selected( $c3m_mbe_featured, 'no' ); ?>>No Way</option>
            <option value="Yes" <?php selected( $c3m_mbe_featured, 'yes' ); ?>>Sure Feature This Video</option>
        </select>
        </p>
        <?php
    }
    
    //hook to save the meta box data
    add_action( 'save_post', 'c3m_mbe_save_meta' );
    function c3m_mbe_save_meta( $post_ID ) {
        global $post;
        if( $post->post_type == "video" ) {
            if ( isset( $_POST ) ) {
                update_post_meta( $post_ID, '_c3m_mbe_featured', strip_tags( $_POST['c3m_mbe_featured'] ) );
            }
        }
    }
    
    }
    

    Here is our cool little featured video post selector we just created:
    enter image description here

    Now lets query the video posts that are featured:

    $args = array(
        'post_type' => 'video',
        'meta_query' => array(
            array(
                'key' => 'c3m_mbe_featured',
                'value' => 'yes',
                'compare' => 'NOT LIKE'
            )
        )
     );
    $query = new WP_Query( $args );