• Resolved rflw

    (@rflw)


    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 method defined in
    wp-admin/includes/class-wp-posts-list-table.php where the action manage_{$post->post_type}_posts_custom_column is 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;
        }    
    }
    
    • This topic was modified 5 years, 9 months ago by rflw.
    • This topic was modified 5 years, 9 months ago by rflw.
    • This topic was modified 5 years, 9 months ago by rflw.
Viewing 10 replies - 1 through 10 (of 10 total)
  • In my plugin, I have define a constant for the post type name, so I only have to change it in one place. But mine has different names for the filters than you have.

    add_filter('manage_edit-'.MY_POSTTYPE.'_columns', 'add_new_custom_columns');
    add_action('manage_'.MY_POSTTYPE.'_posts_custom_column', 'manage_new_custom_columns', 10, 2);

    If this is an error in the documentation, I can bring it to the attention of the docs team, or if you simply misread it, maybe we can make it clearer.

    Thread Starter rflw

    (@rflw)

    I tried your suggestion and I changed filter and action hook name adding variable but there’s no difference. Custom column values are still not visible. I don’t think that’s the problem with hook name.

    As I wrote before, the core function called column_default, which is responsible for calling the do_action("manage_{$post->post_type}_posts_custom_column", $column_name, $post->ID ); seems to be unused.
    I turned it off by commenting completely and still WordPress didn’t return any problems related to the lack of functions.

    Placing die() inside this function results in no reaction, which also suggests that the column_default method is not used (at least for custom post types).

    Filter hook add_filter("manage_{$POST_TYPE}_posts_columns", 'xplugin_manage_posts_columns'); works fine. Custom column is visible but
    action hook called add_action('manage_{$POST_TYPE}_posts_custom_column', 'xplugin_manage_posts_custom_column', 10, 2 ); which is responsible to display column value wont work.

    • This reply was modified 5 years, 9 months ago by rflw.

    But you missed the point of there being two different filters. One is for getting the column into the row and one for filling in the data for that column. If the first isn’t called, the second won’t be invoked.

    Thread Starter rflw

    (@rflw)

    Which filter I missed?
    I have filter that displays custom post column
    and an action that should display a value but does not.

    • This reply was modified 5 years, 9 months ago by rflw.

    Look closely at my example. One starts with manage and the other starts with manage_edit.

    Thread Starter rflw

    (@rflw)

    According to wordpress documentation:

    In WP 3.1, manage_edit-{$post_type}_columns has been supplanted by manage_{$post_type}_posts_columns.

    https://codex.wordpress.org/Plugin_API/Filter_Reference/manage_edit-post_type_columns

    My example should work the same way as yours for WordPress version 3.1 and above (I have currently 5.4.2).

    
    $POST_TYPE = 'xpost';
    // display custom column (works)
    add_filter("manage_{$POST_TYPE}_posts_columns", 'xplugin_manage_posts_columns');
    
    // display custom column value (doesn't work)
    add_action("manage_{$POST_TYPE}_posts_custom_column", 'xplugin_manage_posts_custom_column', 10, 2 );
    

    I tested the filter you suggested (manage_edit..), but it didn’t work for me.

    I’m confused 😉

    • This reply was modified 5 years, 9 months ago by rflw.
    • This reply was modified 5 years, 9 months ago by rflw.
    • This reply was modified 5 years, 9 months ago by rflw.

    Ah, I see what you mean. I haven’t looked at the specifics in awhile, I guess.
    Searching the Code Reference, I see that there are several matches, a filter for getting the column into the list and then an action for populating it. https://developer.wordpress.org/?s=manage+column

    manage_posts_columns
    Filter Hook: Filters the columns displayed in the Posts list table.
    
    manage_{$post_type}_posts_columns
    Filter Hook: Filters the columns displayed in the Posts list table for a specific post type.
    
    manage_posts_custom_column
    Action Hook: Fires in each custom column in the Posts list table.
    
    manage_{$post->post_type}_posts_custom_column
    Action Hook: Fires for each custom column of a specific post type in the Posts list table.

    It seems like you have the right ones, so maybe this statement applies?

    Note that if the custom post type has 'hierarchical' => true, then the correct action hook to use is manage_pages_custom_column.

    But, looking at your code you have false…

    OK, I tried my code on my test site, which is running WP 5.2.7, and it worked perfectly, even with manage-edit. I see the new columns and those columns have content. Then I copied that small plugin to my test site that is running 5.4.2, and it does not work. So something got broken, but it seems like that is a big something and would have been noticed by now.
    I know that there was part of the security fix in the last release (which would be for both of my test sites, which are the latest of their major version), that had to do with changing the name of the filter in the screen options, and they changed it in a way that wasn’t backward compatible. There was a forum topic about it, and a ticket that is going into next release. I wonder if that affects this also, but in a different way, because 5.2.7 works and 5.4.2 does not.

    (I tried changing it to remove the -edit, using the newer name, and it didn’t work either, even on Posts.)
    I deactivated my plugin and installed Admin Columns, and put the ID on Posts and a custom post type, and they both worked fine. Maybe it’s not broken?

    • This reply was modified 5 years, 9 months ago by Joy. Reason: forgot to say I tried the other filter
    Thread Starter rflw

    (@rflw)

    I noticed that my plugin works correctly on external server (latest WordPress version 5.4.2).
    Currently I was using WordPress locally on the Docker, but after realizing the fact that the plug-in works properly on the server I decided to check various images but the result was negative.
    There’s something wrong with WordPress image, but what?

    • This reply was modified 5 years, 9 months ago by rflw.

    I guess you have to isolate what is different.

    Thread Starter rflw

    (@rflw)

    Launching a php container with apache and installing WordPress manually solves this issue.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘No value for custom post type column’ is closed to new replies.