Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I found this on wordpress.org forums. tested it and works fine. When i changed the variables to my own it failed. I checked it thoroughly (!) for hours. i can't see where it's going wrong.

register_post_type( 'book', array(
    'labels' => array(
        'name' => 'Books',
        'singular_name' => 'Book',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Book',
        'edit_item' => 'Edit book',
        'edit' => 'Edit',
        'new_item' => 'New Book',
        'view_item' => 'View Book',
        'search_items' => 'Search Books',
        'not_found' => 'No books found',
        'not_found_in_trash' => 'No books found in Trash',
        'view' => 'View Book'
    ),
    'public' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => true,
    'query_var' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'comments' ),
    'taxonomies' => array('category','post_tag')
    )
);
register_taxonomy(
    'book_category',
    'book',
    array(
        'hierarchical' => true,
        'label' => 'Categoriën',
        'public' => true,
        'query_var' => true,
        'show_tagcloud' => true,
        'rewrite' => Array(
            'slug' => 'book_category')
        )
);
function add_new_columns($defaults) {
    $defaults['book_cats'] = __('Categoriën');
    return $defaults;
}
function add_column_data( $column_name, $post_id ) {
    if( $column_name == 'book_cats' ) {
        $_posttype  = 'book';
        $_taxonomy  = 'book_category';
        $terms      = get_the_terms( $post_id, $_taxonomy );
        if ( !empty( $terms ) ) {
            $out = array();
            foreach ( $terms as $c )
                $_taxonomy_title = esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display'));
                $out[] = "<a href='edit.php?book_category=$_taxonomy_title&post_type=$_posttype'>$_taxonomy_title</a>";
            echo join( ', ', $out );
        }
        else {
            _e('Uncategorized');
        }
    }
}
add_filter( 'manage_book_posts_columns', 'add_new_columns' );
add_action( 'manage_posts_custom_column', 'add_column_data', 10, 2 ); ?>

My edited version (the newly "adjusted" one that now does something!):

register_post_type( 'frog', array(
    'labels' => array(
        'name' => 'Frog',
        'singular_name' => 'Frog',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New frog',
        'edit_item' => 'Edit frog',
        'edit' => 'Edit',
        'new_item' => 'New frog',
        'view_item' => 'View frog',
        'search_items' => 'Search frogs',
        'not_found' => 'No frogs found',
        'not_found_in_trash' => 'No frogs found in Trash',
        'view' => 'View frog'
    ),
    'public' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => true,
    'query_var' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'comments' ),
    'taxonomies' => array('category','post_tag')
    )
);
register_taxonomy(
    'frog_category',
    'frog',
    array(
        'hierarchical' => true,
        'label' => 'Categories',
        'public' => true,
        'query_var' => true,
        'show_tagcloud' => true,
        'rewrite' => Array(
            'slug' => 'frog_category')
        )
);
function add_new_frogcolumns($defaults) {
    $defaults['frog_cats'] = __('Categories');
    return $defaults; //missing as pointed out by scribu
}
function add_frogcolumn_data( $column_name, $post_id ) {
    if( $column_name == 'frog_cats' ) {
        $_posttype  = 'frog';
        $_taxonomy  = 'frog_category';
        $terms      = get_the_terms( $post_id, $_taxonomy );
        if ( !empty( $terms ) ) {
            $out = array();
            foreach ( $terms as $c )
                $_taxonomy_title = esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display'));
                $out[] = "<a href='edit.php?frog_category=$_taxonomy_title&post_type=$_posttype'>$_taxonomy_title</a>";
            echo join( ', ', $out );
        }
        else {
            _e('Uncategorized');
        }
    }
}
add_filter( 'manage_frog_posts_columns', 'add_new_frogcolumns' );
add_action( 'manage_posts_custom_column', 'add_frogcolumn_data', 10, 2 );//this line was also wrong
share|improve this question
    
That script doesn't allow sorting, just displaying a custom column. Also, is that the original script or the modified one? –  scribu Feb 8 '11 at 20:48
    
Check this answer — it enforces ordering on both front-end and backend. –  its_me Jul 16 '12 at 9:30

2 Answers 2

That script doesn't allow sorting, just displaying a custom column. Is that the original script or the modified one?

Note that register_post_type() and register_taxonomy() shouldn't be called directly, but in a callback hooked to the 'init' action. Working version:

function my_init() {
    register_post_type( 'frog', array(
        'labels' => array(
            'name' => 'Frog',
            'singular_name' => 'Frog',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New frog',
            'edit_item' => 'Edit frog',
            'edit' => 'Edit',
            'new_item' => 'New frog',
            'view_item' => 'View frog',
            'search_items' => 'Search frogs',
            'not_found' => 'No frogs found',
            'not_found_in_trash' => 'No frogs found in Trash',
            'view' => 'View frog'
        ),
        'public' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'query_var' => true,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'comments' ),
        'taxonomies' => array('category','post_tag')
        )
    );
    register_taxonomy(
        'frog_category',
        'frog',
        array(
            'hierarchical' => true,
            'label' => 'Categories',
            'public' => true,
            'query_var' => true,
            'show_tagcloud' => true,
            'rewrite' => Array(
                'slug' => 'frog_category')
            )
    );
}

function add_new_frogcolumns($defaults) {
    $defaults['frog_cats'] = __('Categories');//frog_cats relates to? 
    return $defaults;
}
function add_frogcolumn_data( $column_name, $post_id ) {
    if( $column_name == 'frog_cats' ) {
        $_posttype  = 'frog';
        $_taxonomy  = 'frog_category';
        $terms      = get_the_terms( $post_id, $_taxonomy );
        if ( !empty( $terms ) ) {
            $out = array();
            foreach ( $terms as $c )
                $_taxonomy_title = esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display'));
                $out[] = "<a href='edit.php?frog_category=$_taxonomy_title&post_type=$_posttype'>$_taxonomy_title</a>";
            echo join( ', ', $out );
        }
        else {
            _e('Uncategorized');
        }
    }
}

add_action('init', 'my_init');
add_filter( 'manage_frog_posts_columns', 'add_new_frogcolumns' );
add_action( 'manage_frog_custom_column', 'add_frogcolumn_data', 10, 2 );
share|improve this answer
    
It's the original from here wordpress.org/support/topic/… Yeh lack of a function, that did look strange. It does sort though for me, but only when unsullied. –  user3016 Feb 8 '11 at 21:17
    
Well, not much we can do to help if we can't see the faulty version. –  scribu Feb 8 '11 at 23:57
    
i've added the faulty version and tried to add an image of the original working. Not sure if the image hosting is awake though... –  user3016 Feb 9 '11 at 9:33
    
hmm, not getting very far with this. Shame it's not more straightforward. Great having custom post types but sorting them in the admin is quite a nightmare –  user3016 Feb 13 '11 at 13:23
    
Oh, you're missing return $defaults in add_new_frogcolumns(). See updated answer. –  scribu Feb 13 '11 at 17:12

i know this is old, but i stumbled here via google. i was looking for something else, but in case anyone needs it:

http://shibashake.com/wordpress-theme/add-custom-post-type-columns

is some really great info on how to add custom columns for CPTs. strange looking website but some totally badass WP info.

and this is one of justin tadlock's tuts.. and it goes a step further to making things sortable. Custom columns for custom post types

i'm finding that only some columns are sortable.. i think b/c it is a modified query. i have yet to figure out how to sort by taxonomy like i'd like.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.