Share a Custom Taxonomy among different Custom Post Types
-
Hallo!
I would like to associate different Custom Post Types to a same Custom Taxonomy. And I’m looking for the best way to do it. I explain better.I’ve two Custom Post Types: Sculptures and Sketches.
I’ve two Custom Taxonomy: Projects and Materials.Now, the Projects and Materials are associated with Sculptures, that’s the way I organize Sculptures in Projects and depending of the materials they’re done.
The Sketches are related to Projects, the Sculptures are single object born inside a Project.
No problem in creating the Custom Taxonomies and Custom Post Types. But, in the Dashboard I can display Projects and Materials only for Sculptures, or only for Sketches.
Then I can display columns to better visualize Sculptures and Sketches, and I verified that Projects and Materials appear now for every Post Type, even the default. I guess I could do for Pages, too, I got it.
Here the code I’m going to use to visualize those column only where necessary. Please, amend it if you deem it dangerous or not optimal. Thanks!
// Registering the columns for Post Types add_filter( 'manage_posts_columns', 'custom_columns' ); function custom_columns($defaults) { if ( 'sculpture' == get_post_type() || 'sketch' == get_post_type() ) { $defaults['project'] = __('Projects'); } if ( 'sculpture' == get_post_type() ) { $defaults['material'] = __('Materials'); } // for all Post Types $defaults['featured'] = __('Featured'); // this's shared among all Post Types return $defaults; }
This way I display the Projects column only for Sculptures and Sketches; The Materials column only for Sculptures; and the Featured column for every types of Post.
Then, the code to actually make the columns work.
// Actually adds the columns where necessary add_action('manage_posts_custom_column', 'taxonomy_custom_columns', 10, 2); function taxonomy_custom_columns($column_name, $post_id) { global $wpdb; global $post; if ( $column_name == 'project' ) { // name of the taxonomy $tags = get_the_terms($post->ID, 'project'); if ( !empty( $tags ) ) { $out = array(); foreach ( $tags as $c ) { $out[] = "<a href='edit.php?post_type=".get_post_type()."&project=$c->slug'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'project', 'display')) . "</a>"; echo join( ', ', $out ); } } else { _e('Not associated with any Projects'); // No Taxonomy term defined } } else if ( $column_name == 'material' ) { $tags = get_the_terms($post->ID, 'material'); if ( !empty( $tags ) ) { $out = array(); foreach ( $tags as $c ) { $out[] = "<a href='edit.php?post_type=".get_post_type()."&".$column_name."=$c->slug'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, $column_name, 'display')) . "</a>"; echo join( ', ', $out ); } } else { _e('No Related Materials'); } } else if ( $column_name == 'featured' ) { $featured_checkbox = get_post_meta($post->ID, 'featured_checkbox', true); if ( !empty( $featured_checkbox ) ) { $output = '<a href="https://localhost:8888/mariakosh/wp-admin/post.php?post='.$post->ID.'&action=edit">'; $output .= 'Featured'; $output .= '</a>'; print $output; } else { _e(''); } } else { echo '<i>'.__('None').'</i>'; // Only 2 columns, blank now. } }
And till here, everything seems to work just fine.
Now, to have the Custom Taxonomy Project also for Sketches, I would encapsulate the relative code between something like
if ( 'sketch' == get_post_type() ) {
But my doubt is about this lines:
register_taxonomy( 'project', // The name of the custom taxonomy array( 'sculpture' ) )
Can I modify it in…
register_taxonomy( 'project', // The name of the custom taxonomy array( 'sculpture', 'sketch' ) )
…without messing everything up?
Thanks for reading a so long text!!
Carlo
- The topic ‘Share a Custom Taxonomy among different Custom Post Types’ is closed to new replies.