$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>customization - How do I create a featured post within a custom post type?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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?

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?

Share Improve this question asked Feb 21, 2011 at 15:12 Amit ErandoleAmit Erandole 7032 gold badges11 silver badges21 bronze badges 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. – Amit Erandole Commented Feb 21, 2011 at 15:30
Add a comment  | 

2 Answers 2

Reset to default 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:

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

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

Post a comment

comment list (0)

  1. No comments so far