questions

Add Custom Columns to Multiple Custom Post Types

Rate this post

I have added 3 custom post types and for each post types I have ACF Field which i wish to display in the columns for each post types. I have added the code but tits not working if added in foreach loop.

Here is the Code.

$cpt_list = array(
    'book','movie','presentation',
);

foreach ( $cpt_list as $key ) {
    add_filter( "manage_edit-'.$key.'_posts_columns", function($columns) {
        unset(
            $columns['date']
        );
        $columns['custom_pass'] = __( 'Custom Password', 'theme' );
        $columns['date']        = esc_html__( 'Date', 'theme' );

        return $columns;

        }
    );
}

What am I doing wrong?

 

✔️Solution:

I have made a small mistake in the filter hook.

$cpt_list = array(
    'book','movie','presentation',
);

foreach ( $cpt_list as $key ) {
    add_filter( 'manage_'.$key.'_posts_columns', function($columns) {
        unset(
            $columns['date']
        );
        $columns['custom_pass'] = __( 'Custom Password', 'theme' );
        $columns['date']        = esc_html__( 'Date', 'theme' );

        return $columns;

        }
    );
}

Got it working.

Leave a Reply

Your email address will not be published.

Back to top button