• danielwerner23

    (@danielwerner23)


    Hi there,

    i’ve registered a custom post type “product” and a taxonomy “product-category”. How can i display the taxonomies (product-category) in the admin columns of (product)? Like “Foo, Bar”… (comma seperated, similarly to the post categories).

    In the past i’ve added columns in the manage-columns.php with this function:

    add_action('manage_product_posts_custom_column', function ($column, $post_id) {
    	
    	if ('position' === $column) {
    		echo get_field('position');
    	}
    	
    	if ('product-category' === $column) {
    		echo -->> ???? <---;
    	}
    
    }, 10, 2);

    Thanks for help

Viewing 4 replies - 1 through 4 (of 4 total)
  • catacaustic

    (@catacaustic)

    When you use register_taxonomy() there’s an argument there for ‘show_admin_column’. Just set that as true and the column for that taxonomy will show up in the post types that it’s registered to.

    Thread Starter danielwerner23

    (@danielwerner23)

    It doesn’t work:

    register_taxonomy(
    	'product-category',
    	'product',
    	array(
    		'labels' => array(...),
    		'hierarchical' => true,
    		'query_var' => true,
    		'show_admin_column ' => true,
    		'rewrite' => array('slug' => '...')
    	)
    );
    
    register_post_type('product', array(
    	'labels' => array(...),
    	'public' 				=> true,
    	'publicly_queryable'  	=> true,
    	'query_var' 			=> true,
    	'rewrite' 			  	=> true,
    	'hierarchical' 			=> true,
    	'has_archive' 			=> true,
    	'show_in_rest'			=> true,
    	'exclude_from_search' 	=> false,
    	'menu_icon' 			=> 'dashicons-insert',
    	'supports'				=> array('title'),
    	'rewrite' 				=> array('slug' => '...'),
    ));
    Thread Starter danielwerner23

    (@danielwerner23)

    Got it, with my custom solution:

    add_action('manage_product_posts_custom_column', function ($column, $post_id) {
    	if ('taxonomy' === $column) {
    		$terms = get_the_terms($post_id, 'product-category');
    		echo isset($terms) && is_array($terms)
    			?  implode(', ', array_column($terms, 'name'))
    			: '-';
    	}
    }, 10, 2);

    The “show_admin_column” isn’t working.

    Just for anyone who comes across this … ‘show_admin_column’ does work … in the example by the OP above there’s an extra space at the end which is why it didn’t work

    This doesn't work...
    
    'show_admin_column ' => true,
    
    This works...
    
    'show_admin_column' => true,
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add admin column with taxonomy’ is closed to new replies.