fbpx

Adding Custom Columns to Custom Post Types

Adding custom columns to custom post types for your WordPress plugin is a simple 4 step process. For those of you who may not know what custom columns are for custom post types, the picture below shows custom columns for a custom post type listing called Products in WordPress. As you can see, there is a Cost custom column and an Inventory custom column.

For the purposes of this example, we’re using the WordPress Plugin Boilerplate (WPPB) framework when building out WordPress plugins. Check out our post introducing working with the WordPress Plugin Boilerplate if you would like to learn more.

Step 1: Add Custom Columns by hooking into the manage_{$post_type}_posts_column action

Add the following to your _construct method within your class-plugin-name-admin.php file.

// Update the columns shown on the custom post type edit.php view - so we also have custom columns
add_filter('manage_{$post_type}_posts_columns' , array($this,'custom_post_type_columns'));

Step 2: Add the custom_post_type_columns section to add columns to the array

The following function adds the columns that you want to appear in the custom post type listing page (edit.php). As you can see below we deliberately unset several columns and add Custom Column 1, Custom Column 2 and Post Id.

public function custom_post_type_columns($columns){
// Remove Author and Comments from Columns and Add custom column 1, custom column 2 and Post Id
			unset(
				$columns['wpseo-score'],
				$columns['wpseo-title'],
				$columns['wpseo-metadesc'],
				$columns['wpseo-focuskw']
			);
			return array(
							 'cb' => '<input type="checkbox" />',
							 'title' => __('Title'),
							 'custom_column_2' => __('Custom Column 2'),
							 'custom_column_1' => __('Custom Column 1'),
							 'post_id' =>__( 'Post ID'),
					 'date' =>__( 'Date')
					 );
				//return $columns;
		}
	}

Step 3: Add Values to the Custom Columns by hooking into the manage_{$post_type}_posts_custom_column action

Add the code below to your _construct function within your class-plugin-name-admin.php file in order to hook into WordPress to add values to the columns you created in the previous steps.

// this fills in the columns that were created with each individual post's value
add_action( 'manage_{$post_type}_posts_custom_column' , array($this,'fill_custom_post_type_columns'), 10, 2 );

You can learn more about this action at https://codex.wordpress.org/Plugin_API/Filter_Reference/manage_$post_type_posts_columns.

Step 4: Create the fill_custom_post_type_columns function in class-plugin-name-admin.php file

The below function adds values to the 3 custom columns on the custom post type listing. You need to make sure the text in the switch cases match the column names in the array above. For column 1 and column 2, the columns will show a post_meta value if one exists in the database and post_id will show the post id (which is helpful for plugins that use shortcodes). Make sure to add the function below to your class-plugin-name-admin.php file in the WordPress Plugin Boilerplate structure.

public function fill_custom_post_type_columns( $column, $post_id ) {
		// Fill in the columns with meta box info associated with each post
	switch ( $column ) {
	case 'custom_column_1' :
		echo get_post_meta( $post_id , $this->plugin_name.'_custom_column_1' , true ); 
		break;
	case 'custom_column_2' :
		echo get_post_meta( $post_id , $this->plugin_name.'_custom_column_2' , true ); 
			break;
	case 'post_id' :
		echo $post_id; 
			break;
		}
}

One comment on “Adding Custom Columns to Custom Post Types

  • Nick G

    Just here to say that this was extremely helpful… thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *