WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Jan 06 2012

Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 5

Now, in Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 1, we have the registration of the custom post type, custom taxonomy and the addition of the metaboxes. In Part 2, we discussed the Single Page Template for the custom post type. In Part 3, we discussed the Taxonomy Template for the custom taxonomy. In Part 4, we discussed the a page template for the custom post type.

Now this tutorial will discuss Creating Columns in the Edit Custom Post Page. Justin Tadlock has an excellent post on this and is a must read: Custom columns for custom post types. For those, however, who don't like coding, there is a good plugin that will do this work for you. Pippin's Post Type Column Editor from Code Canyon (affil link) is only $12 and worth the money, if you just lovehate coding.

Column Editor

Before, I move forward to how to do this via php and functions.php, let me highlight Pippin's plugin (so if you care about the code skip this and the pictures). With post Type Column Editor you can easily customize the dashboard columns for all your post types. So if you wanted to add thumbnails and excerpts to the columns of posts and/or pages (if pages supports excerpts), then you can easily add it with a few clicks. This plugin gives you a really easy to use way to modify and manage the table columns for your post types. You can display post type entry titles, categories, tags, excerpts, authors, custom meta fields, thumbnails, and custom taxonomies. You can customize the columns for each built-in and custom post type separately with a straight forward drag-and-drop interface.

So when you first register a custom post type, here is what you'll see:

Custom Post Type with No Columns
Initial Custom Post Type

Then when you use the Post Type Column Editor, you see your various options.

Post Type Column Editor Options
Post Type Column Editor Options

For a demonstration check out this Screenr:

This obviously works well with his Easy Content Types plugin which outputs PHP code for you to embed into your plugin and/or theme.

Sometimes it becomes necessary to display more than the usual Title and Date of our post types. In this case, we want to add a new column to the edit post page to allows us to easily see which axle size our disc brakes belong to. There are some great examples of how to do that but below is an example of how we can add our axle size to the page.

First we must add the column. Use the code below and add it to our wps-admin-functions.php file.

[php]
// Add Columns for Disc Brakes Edit Posts Page
add_filter( 'manage_edit-wps_discbrakes_columns', 'wps_discbrakes_columns' ) ;
function wps_discbrakes_columns($column) {
$column = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Disc Brakes' ),
'axlesizes' => __( 'Axle Sizes' ),
'date' => __( 'Date' )
);
return $column;
}
[/php]

If we save this file and upload it, we will now see a new column called Axle Sizes. This is great but there is no content in this column. Now we need to go get the content and display here. Use this code below to do just that.

[php]
// Gets the Taxonomy Terms and retunrs them on the Disc Brakes Edit Posts Page
add_action( 'manage_wps_discbrakes_posts_custom_column', 'manage_wps_discbrakes_columns', 10, 2 );
function manage_wps_discbrakes_columns( $column, $post_id ) {
global $post;
switch( $column ) {
case 'axlesizes' :
$terms = get_the_terms( $post_id, 'wps_axlesizes' );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $term ) {
$out[] = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'wps_axlesizes' => $term->slug ), 'edit.php' ) ), esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'wps_axlesizes', 'display' ) ) );
}

echo join( ', ', $out );
} else {
_e( 'No Axle Sizes' );
}

break;

default :
break;
}
}
[/php]

The code above allows us to select one of the axle sizes and it will only display the disc brakes for that size.
Custom Column for Custom Post Type

Written by Travis Smith · Categorized: Custom Post Types, Genesis

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Scot says

    November 2, 2012 at 1:28 pm

    Travis

    Just a note you used ‘rdwd_discbrakes_columns’ for ‘wps_discbrakes_columns’ above.

    Cheers

    Reply
    • Travis Smith says

      November 12, 2012 at 5:29 pm

      Thanks! Fixed.

      Reply
  2. Scot says

    November 2, 2012 at 3:40 pm

    Travis

    Great tutorial. I’m trying to use the above with multiple taxonomies. Duplicating it for each taxonomy doesn’t work so just wondered if you can provide some direction.

    Reply
    • Travis Smith says

      November 12, 2012 at 5:30 pm

      Your hook is probably wrong. Be sure you are doing this:
      [php]
      add_filter( ‘manage_edit-{taxonomy-name}_columns’, ‘wps_discbrakes_columns’ ) ;
      add_action( ‘manage_{taxonomy-name}_posts_custom_column’, ‘manage_wps_discbrakes_columns’, 10, 2 );
      [/php]

      Reply
  3. Avinash D'Souza says

    January 23, 2013 at 5:41 am

    Hey Travis,

    Just wanted to check with you: is there any way to have the fields in the metaboxes “remember” values?

    Essentially, I’m trying to make an Events CPT and I’d love for WP to give me a dropdown list with pre-populated venues instead of keying it in afresh each time.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in