3

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?

What's the right approach here, guys?

2
  • Can you edit your question and clarify what your $global_cat variable is? The ID of a specific category called "Featured"?
    – Jan Fabry
    Commented Feb 21, 2011 at 15:22
  • no-no..in functions.php I use a global variable called $featured_cat to set one single featured category is all. Actually that's trivial information on my part cause all I really want to know is how to set a featured post within a series of custom post type content. Commented Feb 21, 2011 at 15:30

2 Answers 2

4

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 );
0

Same as with every other post type, really...use a category...or whatever other method you prefer (tag, custom taxonomy, custom metadata/field,...).

3
  • Got it but would love to see an example using custom taxonomy for a custom post type of videos. anyone? Commented Feb 21, 2011 at 16:01
  • 1
    I believe "Sticky posts" are not supported for custom post types. At least this relevant ticket was reverted.
    – Jan Fabry
    Commented Feb 21, 2011 at 16:18
  • @Ian Fabry you're right, edited the answer.
    – wyrfel
    Commented Feb 22, 2011 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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