WordPress.org

Support

Support » How-To and Troubleshooting » Custom field values in admin columns

Custom field values in admin columns

  • dyrk
    Member

    @dyrk

    Hey

    I’ve added a custom post type that’s working fine. For a final tweak I’d like to use show some of the custom field values in columns in the post manager.

    this is the code i have for the columns…but i’m not sure how to get custom field values to show.

    add_action("manage_posts_custom_column", "my_custom_columns");
    add_filter("manage_edit-candidates_columns", "my_candidates_columns");
    
    function my_candidates_columns($columns)
    {
    	$columns = array(
    		"cb" => "<input type=\"checkbox\" />",
    		"title" => "FAB ID",
            "name" => "Name",
    		"area" => "Area"
    	);
    	return $columns;
    }
    
    function my_custom_columns($column)
    {
        global $post;
    
        $cname = get_post_meta($post->ID, 'cname', true);
        $carea = get_post_meta($post->ID, 'carea', true);
    
    	if ("ID" == $column) echo $post->ID;
    	elseif ("name" == $column) echo $cname;
    	elseif ("area" == $column) echo $carea;
    }

    the problem is somewhere with the get_post_meta…not sure what the solution would be.

    any idea would be greatly appreciated

    thanks in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • Peter_L
    Member

    @peter_l

    try using add_meta_box

    dyrk
    Member

    @dyrk

    Thanks

    Can I use that to display existing custom fields in the columns?
    And if so…how? A code snippet would be very useful.

    Many thanks

    Peter_L
    Member

    @peter_l

    hold on, I misunderstood your problem
    add_meta_box has nothing to do with columns

    gonna experiment with columns myself a bit

    Peter_L
    Member

    @peter_l

    try this, works for me

    function my_custom_columns($column){
    	global $post;
    	switch($column){
    		case "name":
    		    $custom = get_post_custom();
          		    echo $custom["cname"][0];
    		    break;
    		case "area":
    		    $custom = get_post_custom();
    		    echo $custom["carea"][0];
    		    break;
    	}
    }

    else, read this http://thinkvitamin.com/dev/create-your-first-wordpress-custom-post-type/

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom field values in admin columns’ is closed to new replies.