I have the following function which is working perfectly for my ‘video’ custom post type to display customized columns in the dashboard.

I have decided the way my plugin will work is to have custom post types created dynamically so I no longer know the exact name of the custom post type and I would like to apply this filter to all custom post type if possible.

    function populate_columns( $column ) {  // populate the custom columns
    if ( 'thumbnail' == $column ) {
        $thumb_array = array('post_type' => 'video');
    $thumbnails = get_posts($thumb_array);
            if ( has_post_thumbnail($thumbnail->ID)) {
              echo '<a href="' . get_edit_post_link( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';  // link thumbnail to edit page
              echo get_the_post_thumbnail($thumbnail->ID, array(100,100));
              echo '</a>';
              }
    }
    elseif ( 'duration' == $column ) {
        $duration_array = array ('post_type'=> 'video');
        $duration = get_post_meta( get_the_ID(), '_my_meta_duration', true );
        if ($duration != '') {
            echo $duration . ' mins';
    }

    }
}

add_action( 'manage_video_posts_custom_column', 'populate_columns' );

I have been going over these two examples from the codex page for get_post_types but I have not been able to figure out how to implement the post_types array into my function.

The call to get post types returns the registered post types.

<?php $post_types=get_post_types(); ?>

Output a list all registered post types

<?php 
$post_types=get_post_types('','names'); 
foreach ($post_types as $post_type ) {
  echo '<p>'. $post_type. '</p>';
}
?>

1 Answer
1

If you want to target all custom post types as well as the built in post types (posts and pages) then you need to use the manage_posts_custom_column hook instead of the manage_{post_type}_posts_custom_column hook which is used to target specific post type columns, subtle difference in naming convention but big difference in how they operate.

Example:

add_action( 'manage_posts_custom_column', 'your_callback'); 

With the above hook, you can still conditionally check for and exclude certain post types, like the built in types if they do not apply to your logic, but this is what you need to target all post types if you don’t know the post type name ahead of time.

  • http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
  • http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_posts_columns

Leave a Reply

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