No value for custom post type column
-
I cannot display column values for a custom post type. I use correct filters and actions according to WordPress documentation.
To make sure I’m doing everything right I’ve prepared a simple plugin based on examples from official documentation to initiate a custom post type and add a custom column.
Unfortunately, the column I added is still not filled with"lorem ipsum".Analyzing WordPress code, I noticed that the
column_default methoddefined in
wp-admin/includes/class-wp-posts-list-table.phpwhere the actionmanage_{$post->post_type}_posts_custom_columnis defined is not executed.https://i.stack.imgur.com/VR9Nj.png
Here’s the full plugin code.
– Post type:
xpost
– Custom column:xpostcol<?php /** * Plugin Name: XPlugin */ add_action('init', 'xplugin_register_post_type'); add_filter('manage_xpost_posts_columns', 'xplugin_manage_posts_columns'); add_action('manage_xpost_posts_custom_column', 'xplugin_manage_posts_custom_column', 10, 2 ); register_activation_hook(__FILE__, 'xplugin_register_activation_hook'); function xplugin_register_post_type() { $labels = array( 'name'=> 'XPosts' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'xpost' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), ); register_post_type( 'xpost', $args ); } function xplugin_register_activation_hook() { xplugin_register_post_type(); flush_rewrite_rules(); } function xplugin_manage_posts_columns($columns) { unset($columns['author']); $new_columns = array('xpostcol' => 'XPost Column'); return array_merge($columns, $new_columns); } function xplugin_manage_posts_custom_column($column, $post_id) { switch ($column) { case 'xpostcol' : echo 'Lorem ipsum'; break; } }
The topic ‘No value for custom post type column’ is closed to new replies.