Creating Custom Post Types and Taxonomies in WordPress

by on October 26, 2015

Creating custom post types and taxonomies in WordPress

Creating Custom Post Types and Taxonomies in WordPress

  •  
  •  
  •  
  •  

< Back to Chapter 6 – Working with Responsive Design in a WordPress Theme

WordPress Development for Noobs is on its seventh chapter. I’ve done my best to outline from the simplest WordPress concepts to the more in-depth concepts, as well as how to utilize WordPress in better ways. We’ve covered themes and child themes, plugins, making your own theme from scratch and working with responsive design. We’re now going to get a little further in-depth with creating a custom post type like ‘Portfolio’ or ‘Work’ and adding taxonomies. Creating Custom Post Type and Taxonomies in WordPress doesn’t need to be complicated, just make sure you know what the site you’re working on needs first.

What you will learn in this article:

What are custom post types and taxonomies and how can we use them to create great websites for ourselves and clients?

WordPress is powerful partly because of its ability to use custom post types, categories and taxonomies. For instance if I need a portfolio custom post type, I can set one up in WordPress, give a separate template and also make a listing that will list out all of the items within that post type that I ‘ask it to.’ For instance, archive-portfolio.php and I can make a custom template to showcase a single portfolio item at single-portfolio.php.  You can also use the WordPress loop on any template with a custom query, but these are items we’ll cover in the next chapter. For now, we’ll show you how to get started setting up these custom post types and ‘taxonomies.’ Taxonomies, like categories or tags, are simply ways to organize your content.

small-blue-legoThe value of custom post types and leveraging them in your WordPress websites

You can have custom post types for everything. Symptoms or treatments for hospitals, slider items, team members, and any kind of directory or listing you’d like to display in a unique or easy way that allows yourself or a client to easily add to. When setting up a new custom post type you can see it as an icon and title in the sidebar of your WordPress Admin. This makes things clean and easy for someone editing their WordPress site. As sites get more complicated, custom post types become more important.

small-blue-legoThe value of custom taxonomies and leveraging them in your WordPress websites

Custom taxonomies are nice ways to organize your content. You could use categories and tags to organize everything because they are easy to add to your custom post types, but I like taxonomies because I can show them underneath a custom post type as essentially a category of categories. For example, I had a client that wanted to be able to add mugs, and as they entered them they could choose shape, glaze color, and handle type easily from the admin because of custom taxonomies. Also with these ‘categories of categories’ I could allow people to search and filter through them with each taxonomy having its own filtering dropdown.

Custom Taxonomies, how and why

 

How to set up a Custom Post Type with and without a plugin

I suggest getting started by learning how to define custom post types without installing a plugin to handle them because they’re fairly easy to do – but I’m not saying you have to do it that way every time. Many people like to have as much control over structural elements like this as possible, which means defining them manually so someone else’s plugin doesn’t unnecessarily hold all the control over their site.

small-blue-legoWorking with Custom Post Types: the Plugin Route

Know that disabling a custom post type plugin like the one I’m about to suggest will essentially disable your site if it is using custom post types in critical areas. Renaming your custom post type will lose the association with your content as well. I’m addressing concerns here that reviewers have brought up in reviewing Custom Post Type UI. The main drawbacks seem to be things that would be true if you had written your Custom Post Type separate from this plugin as well. Try this plugin, however, and read the reviews, it’s called Custom Post Type UI. Here’s a screenshot of adding a new custom post type:

Custom Post Type UI, reviews and alternative

I found the interface easy enough to use. Essentially when adding new custom post types it takes some time to define all the items, and you’ll have to do that no matter if you use a plugin or the code snippets below. Adding via this plugin is your choice, just make an informed decision.

small-blue-legoWorking with custom post types without someone else’s plugin – with code snippets

I personally add my custom post types as a custom written plugin, so that it’s separate from the site’s theme. The idea is to make sure your content and structure can live happily on their own. For instance, if you changed themes, then all of your portfolio items of symptoms for a medical site wouldn’t be lost with your theme.

So to add a new custom post type you need to define its title, singular and plural uses, if it will be hierarchical, if you want to have a custom dash icon, and a couple other things, and you’re good to go. Here’s an example of one. To activate this, save it as plugin-name.php, zip it up and upload it to your site as a plugin. Then activate it.

 


<?php
/*
Plugin Name: Portfolio Items
Plugin URI: https://timbdesign.com/
Description: Declares a plugin that will create a custom post type displaying portfolio items.
Version: 1.0
Author: Tim Brown
Author URI: https://timbdesign.com/
License: GPLv2
*/
?>

// Creates Portfolio Items Custom Post Type
function portfolio_items_init() {
 $args = array(
 'label' => 'Portfolio Items',
 'public' => true,
 'show_ui' => true,
 'capability_type' => 'post',
 'hierarchical' => false,
 'rewrite' => array('slug' => 'portfolio-items'),
 'query_var' => true,
 'menu_icon' => 'dashicons-desktop',
 'supports' => array(
 'title',
 'editor',
 'excerpt',
 'trackbacks',
 'custom-fields',
 'comments',
 'revisions',
 'thumbnail',
 'author',
 'page-attributes',)
 );
 register_post_type( 'portfolio-items', $args );
}
add_action( 'init', 'portfolio_items_init' );

You can also swap out all of these items with what you’re needing in a custom post type. A couple things to make sure of are to make the function at the beginning and  init / action at the end both the same, and only use underscores, not dashes in these spots.

If you want to easily generate these things you can use Custom Post Type Generator by Thememergency.

How to set up a custom taxonomy with and without a plugin

 

small-blue-legoCustom Taxonomies: The plugin Route

The same plugin above can allow you to quickly execute custom taxonomies. Custom Post Type UI is great for this, and I might start using it in the future just because of the speed it allows for in creating these.

small-blue-legoCustom taxonomies without a plugin

 

Once again when I say without a plugin, I’m actually suggesting you essentially place this in your own simple plugin, containing any Custom Post Types and taxonomies. In this situation we’d add this to the same plugin that we created above, and these topics would apply to whatever post type you enter where this snippet below says ‘portfolio-items.’

 

//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires

add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );

function create_topics_nonhierarchical_taxonomy() {

// Labels part for the GUI

  $labels = array(
    'name' => _x( 'Topics', 'taxonomy general name' ),
    'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Topics' ),
    'popular_items' => __( 'Popular Topics' ),
    'all_items' => __( 'All Topics' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Topic' ), 
    'update_item' => __( 'Update Topic' ),
    'add_new_item' => __( 'Add New Topic' ),
    'new_item_name' => __( 'New Topic Name' ),
    'separate_items_with_commas' => __( 'Separate topics with commas' ),
    'add_or_remove_items' => __( 'Add or remove topics' ),
    'choose_from_most_used' => __( 'Choose from the most used topics' ),
    'menu_name' => __( 'Topics' ),
  ); 

// Now register the non-hierarchical taxonomy like tag

  register_taxonomy('topics','portfolio-items',array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'topic' ),
  ));
}

If you want to generate this and not change out everything in this snippet, you can use GenerateWP’s easy-to-use Custom Taxonomy Generator.

small-blue-legoGetting the Most out of Custom Taxonomies: Some Final Notes.

Always think through the categories and sub-categories before creating this whole infrastructure. I use this particularly for things I want to sort or show based on different attributes. I make sure I know the key categories and sub-categories for the items based on the client’s needs. Then, and only then, do I create custom post types and taxonomies. Otherwise content might need to be re-added if I have to change them later.

Tweet this!

Thank you so much for reading ‘Creating Custom Post Types and Taxonomies in WP’ in the WordPress for Noobs series, I love creating WordPress sites in Minneapolis for clients. Reach out to me if you need anything and leave a comment if you have any questions or anything to add to the conversation. Thank you!

Green LegoUtilizing the WordPress loop to display Posts, in & out of Archives

 


  •  
  •  
  •  
  •  

Download my E-book "WordPress Development for Noobs" for free right now:

  • This field is for validation purposes and should be left unchanged.
5 E-mails a year: Only the best of web design & marketing

Ready to get started?

Get a free consultation now