webSIGHTdesigns - Web Design, Web Development, Web Hosting

Custom Post Types in WordPress

Posted on Thursday, April 23rd, 2015 at 10:37 pm
by webSIGHTdesigns

Setting up Custom Post Types in the WordPress CMS.

Custom Post Types are post types that you can create in WordPress. By default WordPress has 5 post types:

  • Post (Post Type: 'post')
  • Page (Post Type: 'page')
  • Attachment (Post Type: 'attachment')
  • Revision (Post Type: 'revision')
  • Navigation menu (Post Type: 'nav_menu_item')

You may add you own post types as well. For example, you may want to add a custom post type for Videos and then display those videos on a template page in a carousel. The best way to set up a Custom Post Type is by creating a plugin, but you can also place these examples into your theme's functions.php file.

You can register your new Custom Post Type with the register_post_type() function:

/**
* Custom Post Type: Videos
*/
function my_custom_post_videos() {
  $labels = array(
    'name'               => _x( 'Videos', 'post type general name' ),
    'singular_name'      => _x( 'Video', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Video' ),
    'edit_item'          => __( 'Edit Video' ),
    'new_item'           => __( 'New Video' ),
    'all_items'          => __( 'All Videos' ),
    'view_item'          => __( 'View Video' ),
    'search_items'       => __( 'Search Videos' ),
    'not_found'          => __( 'No videos found' ),
    'not_found_in_trash' => __( 'No videos found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Videos'
    );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our videos and video specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
    'has_archive'   => false,
    );
  register_post_type( 'videos', $args );
}
add_action( 'init', 'my_custom_post_videos' );

The $args array stores some important settings regarding what features your new Custom Post Type will support. You can view the full range of options on the WordPress Codex page for register_post_type.

The most important settings are the 'supports' and the 'menu_position' values.

The 'menu_position' value determines where in the WordPress admin's menu the link to your Custom Post Type will appear. Set this to 5 to show the menu item below the 'Posts' link.

The 'supports' array determines what default WordPress features your Custom Post Type will support. For instance, removing the 'editor' value from this array would remove the WYSIWYG editor, or removing the 'thumbnail' value would remove the Featured Image section.

Next you can set up custom taxonomies for your Custom Post Type:

/**
* Custom Post Taxonomies: Videos
*/
add_action( 'init', 'my_taxonomies_videos', 0 );
function my_taxonomies_videos() {
  $labels = array(
    'name'              => _x( 'Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Categories' ),
    'all_items'         => __( 'All Categories' ),
    'parent_item'       => __( 'Parent Category' ),
    'parent_item_colon' => __( 'Parent Category:' ),
    'edit_item'         => __( 'Edit Category' ),
    'update_item'       => __( 'Update Category' ),
    'add_new_item'      => __( 'Add New Category' ),
    'new_item_name'     => __( 'New Category' ),
    'menu_name'         => __( 'Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'query_var' => true,
  );
  register_taxonomy( 'videos_category', 'videos', $args );
}

Next you can set up a custom field for your Videos to store the URL using the add_meta_box() function:

/**
* Custom Post Type Metabox: Videos
*/
add_action( 'add_meta_boxes', 'videos_url' );
function videos_url() {
  add_meta_box(
    'videos_url',
    __('Title'),
    'videos_url_content',
    'videos',
    'normal',
    'high'
    );
}
function videos_url_content( $post ) {
  $value = get_post_meta( $post->ID, 'videos_url', true );
  wp_nonce_field( 'videos_url_save_nonce', 'videos_url_content_nonce' );
  echo '<label for="videos_url"></label>';
  echo '<input id="videos_url" name="videos_url" size="54" type="text" value="' . esc_attr( $value ) . '" placeholder="Enter video URL here" />';
}

Now add an action containing the update_post_meta() function to save your Video URLs entered into the post meta box you created above:

/**
* Saving Custom Post Type Metabox: Videos
*/
add_action( 'save_post', 'videos_title_save' );
function videos_title_save( $post_id ) {

  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;

  if( !isset( $_POST['post_type'] ) ) {
    return;
  }

  if ( !wp_verify_nonce( $_POST['videos_title_content_nonce'], 'videos_title_save_nonce' ) )
    return;

  if ( 'page' == $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_page', $post_id ) )
      return;
  } else {
    if ( !current_user_can( 'edit_post', $post_id ) )
      return;
  }
  $videos_title = $_POST['videos_title'];
  update_post_meta( $post_id, 'videos_title', $videos_title );
}

You can add the Category column to the list of posts you see when you click the Videos menu item in the WordPress admin:

/**
* Showing Custom Taxonomies in Columns: Videos
*/
add_filter( 'manage_taxonomies_for_videos_columns', 'videos_type_columns' );
function videos_type_columns( $taxonomies ) {
  $taxonomies[] = 'videos_category';
  return $taxonomies;
}

Now that you've set up your Custom Post Type for Videos, you can change the default placeholder text of the Video title as well, by adding an enter_title_here filter:

/**
* Changing default title placeholder text
*/
function change_default_title( $title ){
  $screen = get_current_screen();
  if  ( $screen->post_type == 'videos' ) {
    return 'Enter video name here';
  } else {
    return 'Enter title here';
  }
}
add_filter( 'enter_title_here', 'change_default_title' );

Be sure to leave the default value in the else section of the if statement. This is the value that will be used for your posts. You can add any other Custom Post Types you set up here as well to set their default placeholder text for the title field.

To filter by the Category column in the list of posts, add a parse_query filter:

add_action( 'restrict_manage_posts', 'taxonomy_filter_restrict_manage_posts' );
function taxonomy_filter_restrict_manage_posts() {
  global $typenow;

  $post_types = get_post_types( array( '_builtin' => false ) );

  if ( in_array( $typenow, $post_types ) ) {
    $filters = get_object_taxonomies( $typenow );

    foreach ( $filters as $tax_slug ) {
      $tax_obj = get_taxonomy( $tax_slug );
      wp_dropdown_categories( array(
        'show_option_all' => __('Show All '.$tax_obj->label ),
        'taxonomy'    => $tax_slug,
        'name'      => $tax_obj->name,
        'orderby'     => 'name',
        'selected'    => (isset($_GET[$tax_slug]) ? $_GET[$tax_slug] : 0),
        'hierarchical'    => $tax_obj->hierarchical,
        'show_count'    => false,
        'hide_empty'    => true
        ) );
    }
  }
}
add_filter( 'parse_query', 'taxonomy_filter_post_type_request' );
function taxonomy_filter_post_type_request( $query ) {
  global $pagenow, $typenow;
  if ( 'edit.php' == $pagenow ) {
    $filters = get_object_taxonomies( $typenow );
    foreach ( $filters as $tax_slug ) {
      $var = &$query->query_vars[$tax_slug];
      if ( isset( $var ) ) {
        $term = get_term_by( 'id', $var, $tax_slug );
        if( isset($term->slug) ) $var = $term->slug;
      }
    }
  }
}

As you can see, Custom Post Types are a very powerful tool and a great and user-friendly way for your users to manage different types of content in WordPress.

Please Sign In

Please sign in to post a comment.

Web Development

View details »

Web Hosting

View details »

Our Portfolio

View portfolio »

WebSight Designs webSIGHTdesigns preferred email webSIGHTdesigns United States United States