15

I have built a custom post type called article and the information given on the admin summary screen is sparse. I was able to add the featured image post thumbnail image using the http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column from a tutorial.

However I would like to be able to get an overview of the categories and sub categories that these posts have had assigned to them on the admin page. ie adding a column for that part?

Here is the code that I have used to register the taxonomy in the custom post types code

1

3 Answers 3

24

The register_taxonomy function has a parameter called show_admin_column that will handle adding a column. Have you tried that?

eg:

register_taxonomy(
    'my_tax, 
    'post_type', 
    array(
        'label'             => 'My Taxonomy',
        'show_admin_column' => true,
        )
);
2
  • 1
    Please, add code and explain how using it answers the quesiton. If you just want to ask something to the OP, use the comments.
    – cybmeta
    Dec 21, 2016 at 15:00
  • It's 2020 and the answer is still working. Thanks for the help. Upvote from my side. Sep 23, 2020 at 19:38
8

After some searching, I have found a solution using the manage_edit-${post_type}_columns filter and the manage_${post_type}_posts_custom_column action.

The columns are created with the filter and then the column is populated with the action. I assume additional columns could be added and populated quite easily using the ideas in this link http://justintadlock.com/archives/2011/06/27/custom-columns-for-custom-post-types

add_filter('manage_edit-article_columns', 'my_columns');
function my_columns($columns) {
    $columns['article_category'] = 'Category';
return $columns;
}

add_action( 'manage_article_posts_custom_column', 'my_manage_article_columns', 10, 2 );

function my_manage_article_columns( $column, $post_id ) {
global $post;

switch( $column ) {

    /* If displaying the 'article_category' column. */
    case 'article_category' :

        /* Get the genres for the post. */
        $terms = get_the_terms( $post_id, 'article_category' );

        /* If terms were found. */
        if ( !empty( $terms ) ) {

            $out = array();

            /* Loop through each term, linking to the 'edit posts' page for the specific term. */
            foreach ( $terms as $term ) {
                $out[] = sprintf( '<a href="%s">%s</a>',
                    esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'article_category' => $term->slug ), 'edit.php' ) ),
                    esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'article_category', 'display' ) )
                );
            }

            /* Join the terms, separating them with a comma. */
            echo join( ', ', $out );
        }

        /* If no terms were found, output a default message. */
        else {
            _e( 'No Articles' );
        }

        break;

    /* Just break out of the switch statement for everything else. */
    default :
        break;
}
}
0

Nearly the same question: i want a extra column "categorie" in the admin screen. "categorie" is a taxonomy made with Divi Machine. The slug is art_category In my child theme functions.php i managed to make ACF fields visible, even sortable but those are ACF fields. The column Categorie is visible. However there are no values.

It must be in here somewhere:

    if ( 'artcategory' === $column ) {
    $artcategory = get_post_meta( $post_id, 'art_category', true );

    if ( ! $artcategory ) {
      _e( 'n/b' );
    } else {
      echo $artcategory;
    }
  }


What am i doing wrong.....
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.