How To Create Custom Post Types and Taxonomies
(( WordPress Custom Post Types and Taxonomies: How To Create, Which Ones to Define, How to Use on Your Political Campaign Website )){{ AGREE (something reader will agree with), Promise (it can be easier), Preview (in this post / video) }}{{ TO THINK ABOUT: Where do I put what the advantages of including custom post types and taxonomies??... Now that they are set up, what do you do?? }}Creating custom post types and taxonomies seems intimidating. Taking the time to learn how these data structures work can ensure that website content appears consistently and enables on-the-fly changes by campaign staff. This post for intermediate to advanced WordPress users covers how to create custom post types and taxonomies, identifies which ones are useful for a political campaign website, and {{how to use the data structures.}}Importance of implementing: Depends on theme. If theme has a "Testimonials" feature built in already, may want to ignore.
Minimum Requirements
- Site using WordPress
- Theme design handled by a child theme
How to Create WordPress Custom Post Types and Taxonomies
Custom post types can be created using a plugin or by creating from scratch.
Pros of Creating from Scratch
- One less plugin to keep updated
- Not subject to whims of plugin developer: May stop supporting plugin, may drastically change plugin
- Less chance of potential conflicts with other plugins
- Depending on plugin, may have fee(s) associated with use
Cons of Creating from Scratch
- Requires some coding which may be intimidating
- If change the theme at some future point, will need to remember to move appropriate code over to new theme
Creating the Right Plugins and Taxonomies for a Campaign Website
This will vary by campaign, but general starting points are:
Custom Post Types
- Policies
- Endorsements
- Testimonials
- Press Releases
Taxonomies
- Featured
Defining the Custom Post Types and Taxonomies in functions.php
Custom post types and taxonomies are defined in the functions.php file kept in the theme's directory. [php]/*Setting up my taxonomies*/$labels = array( 'name' => 'Featured', 'singular_name' => 'Featured', 'search_items' => 'Search Featured', 'popular_items' => 'Popular Featured', 'all_items' => 'All Featured', 'parent_item' => 'Parent Featured', 'edit_item' => 'Edit Featured', 'update_item' => 'Update Featured', 'add_new_item' => 'Add New Featured', 'new_item_name' => 'New Featured', 'separate_items_with_commas' => 'Separate featured with commas', 'add_or_remove_items' => 'Add or remove featured', 'choose_from_most_used' => 'Choose from most used featured' );$args = array( 'label' => 'Featured Placement', 'labels' => $labels, 'public' => true, 'hierarchical' => false, 'show_ui' => true, 'show_in_nav_menus' => true, 'args' => array( 'orderby' => 'term_order' ), 'rewrite' => array( 'slug' => 'featured', 'with_front' => false ), 'query_var' => true);//register_taxonomy( 'featured', 'startups', $args );register_taxonomy( 'featured', array('pressreleases','endorsements','testimonials','policies'), $args );/*Registering my post types (must be done after setting up taxonomies)*/register_post_type( 'pressreleases', array( 'labels' => array( 'name' => __( 'Press Releases' ), 'singular_name' => __( 'Press Release' ) ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'revisions', 'tags' ), 'rewrite' => array( 'slug' => 'pressreleases', 'with_front' => false ), 'has_archive' => true, 'publicly_queryable' => true, ));register_post_type( 'endorsements', array( 'labels' => array( 'name' => __( 'Endorsements' ), 'singular_name' => __( 'Endorsement' ) ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'revisions', 'tags' ), 'rewrite' => array( 'slug' => 'endorsements', 'with_front' => false ), 'has_archive' => true, 'publicly_queryable' => true, ));register_post_type( 'testimonials', array( 'labels' => array( 'name' => __( 'Testimonials' ), 'singular_name' => __( 'Testimonial' ) ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'revisions', 'tags' ), 'rewrite' => array( 'slug' => 'testimonials', 'with_front' => false ), 'has_archive' => true, 'publicly_queryable' => true, ));register_post_type( 'policies', array( 'labels' => array( 'name' => __( 'Policies' ), 'singular_name' => __( 'Policy' ) ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'revisions', 'tags' ), 'rewrite' => array( 'slug' => 'policies', 'with_front' => false ), 'has_archive' => true, 'publicly_queryable' => true, ));add_action('init', 'add_tags_to_custom_post_types');function add_tags_to_custom_post_types(){ register_taxonomy_for_object_type('post_tag', 'pressreleases'); register_taxonomy_for_object_type('post_tag', 'endorsements'); register_taxonomy_for_object_type('post_tag', 'testimonials'); register_taxonomy_for_object_type('post_tag', 'policies');}function post_type_tags_fix($request){ if ( isset($request['tag']) && !isset($request['post_type']) ) $request['post_type'] = 'any'; return $request;} add_filter('request', 'post_type_tags_fix');[/php]
Next Steps
Now that custom post types and taxonomies are created, the next steps are to create forms to collect the information {{ TO DO: Insert Link }} and to create WordPress templates to use the data {{ TO DO: Insert Link }}.