Custom post types columns strange issue

I have 4 custom post types in my theme. When I add a description column (the_excerpt) for them I get some really strange behavior.

The description column repeats 4 times on each post on every custom post type. Also get a PHP notice that I can’t get rid of.

Read More

Like this:

Notice: Trying to get property of non-object in H:htdocswp-contentthemesmythemeadmin_includesotherexcerpt.php on line 10
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Notice: Trying to get property of non-object in H:htdocswp-contentthemesmythemeadmin_includesotherexcerpt.php on line 10
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Notice: Trying to get property of non-object in H:htdocswp-contentthemesmythemeadmin_includesotherexcerpt.php on line 10
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Notice: Trying to get property of non-object in H:htdocswp-contentthemesmythemeadmin_includesotherexcerpt.php on line 10
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Here’s my code for the custom post type. The code is the same for each one apart from the post type name.

<?php
    add_action('init', 'testimonials_register');
    function testimonials_register() {
        $labels = array(
            'name' => _x('Testimonials', 'post type general name'),
            'singular_name' => _x('Testimonial', 'post type singular name'),
            'add_new' => _x('Add New', 'Testimonial'),
            'add_new_item' => __('Add New Testimonial'),
            'edit_item' => __('Edit Testimonial'),
            'new_item' => __('New Testimonial'),
            'view_item' => __('View Testimonial'),
            'search_items' => __('Search Testimonials'),
            'not_found' =>  __('Nothing found'),
            'not_found_in_trash' => __('Nothing found in Trash'),
            'parent_item_colon' => ''
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'menu_icon' => get_stylesheet_directory_uri() . '/images/icons/testimonials-admin.png',
            'rewrite' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => null,
            'rewrite' => array(
                'slug' => 'testimonials',
                'with_front' => FALSE,
            ),
            'supports' => array('title','editor','thumbnail')
        );
        register_post_type( 'testimonials' , $args );
    }

    add_action("manage_posts_custom_column",  "testimonials_custom_columns");
    add_filter("manage_edit-testimonials_columns", "testimonials_edit_columns");

    function testimonials_edit_columns($columns){
        $columns = array(
            "cb" => "<input type="checkbox" />",
            "title" => "Testimonial Title",
            "description" => "Description"
        );
        return $columns;
    }

    function testimonials_custom_columns($column){
        global $post;
        switch ($column)
        {
            case "description":
                the_excerpt();
                break;
        }
    }
?>

I can’t see why the description column would repeat for as many times as I have custom post types. Also I really can’t figure out the PHP notice.

I’m developing locally on LAMP server and my WP version is 3.3.

Would really appreciate some help. Thanks!

Related posts

Leave a Reply

1 comment

  1. Worked it out! Since WP version 3.1 the hook for adding columns to custom post types has changed.

    Before 3.1 it was:

    add_action("manage_posts_custom_column",  "testimonials_custom_columns");
    

    After 3.1 you have to use this:

    add_action("manage_testimonials_posts_custom_column",  "testimonials_custom_columns");
    

    So you have to specify the custom post type name in the manage_post_custom_column hook.

    Hopefully this answer will save someone some time.