WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding Custom columns for Custom Post type (2 posts)

  1. Daniel Gilfoy
    Member
    Posted 4 years ago #

    I've been having some difficulty getting Custom Columns working.

    I set up a simple "test" column like so:

    function post_type_testcols($cols){
    	$cols['testytest'] = "Test Column";
    	return $cols;
    }
    
    function fill_cols($col){
    	switch($col){
    		case "testytest":
    			echo "Test Works";
    			break;
    	}
    }

    And not only does the new column show up under normal Posts just fine, so does the "Test Works" message, on each row. Great!

    Now I add a simple Custom Post type:

    function test_post_type_init() {
    
    	$labels = array(
    		'name'=>__("Tests"),
    		'singular_name'=>__("Test"),
    		'add_new'=>__('New Test'),
    		'add_new_item'=>__('Add New '),
    		'edit_item'=>__('Edit Test'),
    		'new_item'=>__('Add New Test'),
    		'view_item'=>__('View Test'),
    		'search_items'=>__('Search Tests')
    	);
    
    	$supports = array('title','excerpt','thumbnail','comments');
    
    	$args = array(
    		'label'=> __('tests'),
    		'labels'=>$labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => true,
    		'menu_position' => 4,
    		'supports' => $supports
    	);
    
    	register_post_type('tests',$args);
    }
    add_action( 'init', 'test_post_type_init');

    Column name shows up under Tests Post type, but the "Test Works" does not show up.

    Nothing I've tried works.

    Anybody have luck with Custom Post types and adding custom columns to them?

  2. Daniel Gilfoy
    Member
    Posted 4 years ago #

    Crap, been searching for the solution for 4 days now and I just ran across this comment after posting this and it worked.

    The quote as follow:

    It’s good to note that you can’t change the columns of a post type that’s set to “Page” or has “hierarchical => true”.

    Which is weird, but I don't need it to be hierarchical so problem solved! .

Topic Closed

This topic has been closed to new replies.

About this Topic