Support

Account

Home Forums Backend Issues (wp-admin) Custom Admin Columns for Multiple CPTs

Helping

Custom Admin Columns for Multiple CPTs

    • mtm

    • August 14, 2019 at 2:28 am

    I am trying to define different admin columns for different Custom Post Types, each using a different set of ACF fields. I’ve declared my CPTs as follows:

    add_action('init', 'all_custom_post_types');
    
    function all_custom_post_types() {
      $types = array(
        //Events
        array('the_type' => 'event',
              'single' => 'Event',
              'plural' => 'Events'),
        //Releases
        array('the_type' => 'release',
              'single' => 'Release',
              'plural' => 'Releases'),
      );
    
    foreach ($types as $type) {
      $the_type = $type['the_type'];
        $single = $type['single'];
        $plural = $type['plural'];
      $labels = array(
          'name' => _x($plural, 'post type general name'),
          'singular_name' => _x($single, 'post type singular name'),
          'add_new' => _x('Add New', $single),
          'add_new_item' => __('Add New '. $single),
          'edit_item' => __('Edit '.$single),
          'new_item' => __('New '.$single),
          'view_item' => __('View '.$single),
          'search_items' => __('Search '.$plural),
          'not_found' =>  __('No '.$plural.' found'),
          'not_found_in_trash' => __('No '.$plural.' found in Trash'),
          'parent_item_colon' => ''
        );
        $args = array(
          'labels' => $labels,
          'public' => true,
          'publicly_queryable' => true,
          'show_ui' => true,
          'query_var' => true,
          'rewrite' => true,
          'capability_type' => 'post',
          'hierarchical' => false,
          'has_archive' => true,
          'menu_position' => 5,
          'supports' => array('title','editor','thumbnail'),
          'show_in_menu' => true,
          'show_in_nav_menus' => true
        );
    
        register_post_type($the_type, $args);
      }
    }

    All good so far. The trouble comes when attempting to make these sortable at the “All posts” level of the admin area by specific ACF fields. Events and Releases have completely different sets of data.

    Looking at this post – https://www.elliotcondon.com/advanced-custom-fields-admin-custom-columns/ – I can see how I would do this for a single CPT, but what if I want the “All Events” screen to show (say) date, venue and location (all ACF fields) but the “All Releases” screen to offer columns for release year, artist and label?

    Is it possible to specify somewhere when declaring custom columsn which CPT they should apply to, and do this several times over, once per CPT?

  • There are 2 filters in WP that let you do this, one to add the columns and another to add the column content

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

    column content: https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.