0

I made a plugin which adds a "member" custom post type. I customized the edit view (with the editor hook) to get only the fields I want and store them in the database. I have fields like "name, surname, birthdate..."

What I want now is to make some of these fields appear in the "browse page" of my member custom post type. I want to remove for example "Thumbs, SEO, meta desc..." columns and see columns with the fields of my custom type.

I tried to play with the manage_posts_custom_column hook, but without success. Can anyone help?

1 Answer 1

1

Here is an sample of my plugin's code where I set a custom post type "aei_team_member" and add 2 columns (Prénom and Photo where Photo is a thumbnail) in admin list using :

manage_${post_type}_posts_columns

and

manage_{$post_type}_posts_custom_column

Further information can be found here: https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

I hope it helped!

/**
 * Adds a thumbnail column in the admin panel.
 */
function add_aei_team_member_columns($columns) {
  $new = array();
  foreach($columns as $key => $title) {
    if( $key == 'taxonomy-aei_team_member_group' )
      $new['firstname'] = 'Prénom';
    if( $key == 'date' )
      $new['thumbnail'] = 'Photo';
    $new[$key] = $title;
  }
  return $new;
}

add_filter('manage_aei_team_member_posts_columns' , 'add_aei_team_member_columns');

/**
 * Adds a thumbnail column in the aei_team_member list in admin panel.
 */
function thumbnail_custom_columns($column_name, $id) {
  switch( $column_name ) {
    case 'thumbnail':
      if( has_post_thumbnail() ) {
        the_post_thumbnail( array( 80, 80 ) );
      } else { ?>
        <?php $upload_dir = wp_upload_dir(); ?>
        <?php $upload_dir = $upload_dir['baseurl']; ?>
        <img src="<?php echo get_stylesheet_directory_uri(); ?>/img/default-avatar.png" height="80" width="80" />
      <?php }
      break;
    case 'firstname':
      echo get_post_meta( $id, 'member_firstname', true );
      break;
  }
}

add_action('manage_aei_team_member_posts_custom_column', 'thumbnail_custom_columns', 10, 2);

It gives me this:

new columns

To delete a column, just use unset() within this function :

function my_columns_filter( $columns ) {
   unset($columns['author']);
   unset($columns['categories']);
   unset($columns['tags']);
   unset($columns['comments']);
   return $columns;
}

// Filter pages
add_filter( 'manage_edit-page_columns', 'my_columns_filter',10, 1 );    

// Filter Posts
add_filter( 'manage_edit-post_columns', 'my_columns_filter',10, 1 );

// Custom Post Type
add_filter( 'manage_edit-CUSTOMPOSTTYPE_columns', 'my_columns_filter',10, 1 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.