Search code examples
phpwordpressadvanced-custom-fieldscustom-post-type

How to display a custom field in the dashboard posts list( replacing the title )


I'm using WordPress and I did create a custom post type with several custom fields ( using ACV, advanced custom fields ) I have hidden all the basic WordPress fields like title, content editor thumbnails, etc, etc so I leave just my custom fields for the creation of a new post.

Since the title is not filled at the creation of the post, I get a list of post with every title set as “auto draft” The thing that I don't want obviously.

My question is simple :

Is it possible and if yes how to replace the title with one of my custom fields in the dashboard post list.

I searched everywhere but I couldn't find an answer.

Sorry for my English, it’s not my native tongue, I hope You understand what I basically want to do.

Thanks for your time to read my question. Have a good day


Solution

  • I managed to find a solution myself.

    Let us say that you have a custom post type named "test" with 3 custom fields named one, two and three. And you want to remove the title and the date, show the content of one, two, and three in the post list table.

    First you have to create a function that removes the title and the date, and also creates the new columns.

    function custom_columns($columns)
    {
        unset($columns['title']);
        unset($columns['date']);
        return array_merge(
            $columns,
            array(
                'one' => __('One'),
                'two' => __('Two'),
                'three' => __('Three')
            )
        );
    }
    add_filter('manage_test_posts_columns', 'custom_columns');
    

    Then you need to display the custom field content in the post list table:

    function display_custom_columns($column, $post_id)
    {
        switch ($column) {
            case 'one':
                echo get_post_meta($post_id, 'one', true);
                break;
            case 'two':
                echo get_post_meta($post_id, 'two', true);
                break;
            case 'three':
                echo get_post_meta($post_id, 'three', true);
                break;
        }
    }
    add_action('manage_test_posts_custom_column', 'display_custom_columns', 10, 2);
    

    Further reading