WordPress.org

Ready to get started?Download WordPress

Forums

Custom field values in admin columns (5 posts)

  1. dyrk
    Member
    Posted 3 years ago #

    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

  2. Peter_L
    Member
    Posted 3 years ago #

    try using add_meta_box

  3. dyrk
    Member
    Posted 3 years ago #

    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

  4. Peter_L
    Member
    Posted 3 years ago #

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

    gonna experiment with columns myself a bit

  5. Peter_L
    Member
    Posted 3 years ago #

    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/

Topic Closed

This topic has been closed to new replies.

About this Topic