Archived topic
Add Taxonomy to GP Elements Post Type?
5 replies · Started by Randy on December 14, 2022
Hey guys!
I'm sure this has already come up, but I couldn't find any existing topics, so I apologize if this is a repeat.
Is it possible to add a taxonomy to the gp_elements post type? Similar to how each element has a "Type" assigned to it?
I have a site with a lot of elements, and it would be awesome to be able to categorize/sort them on the admin edit screen (/wp-admin/edit.php?post_type=gp_elements).
Could this be done via the register_post_type_args filter? If so, what would the code look like to add a generic taxonomy to it?
add_filter( 'register_post_type_args', function( $args, $post_type ) {
if ( 'gp_elements' === $post_type ) {
// Is this possible?
}
return $args;
}, 10, 2 );
Any info or instruction would be greatly appreciated! Thanks!
Hi there,
i am not sure of the ramifications of doing this, but you can simply register_taxonomy for the gp_elements post type.
Heres a quick and dirty example of the code. It should at least add an Admin column for your to sort by.
// Register Custom Taxonomy
function Element_Category() {
$labels = array(
'name' => _x( 'Element Categories', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Element Category', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Element Category', 'text_domain' ),
'all_items' => __( 'All Element Categories', 'text_domain' ),
'parent_item' => __( 'Parent Element Category', 'text_domain' ),
'parent_item_colon' => __( 'Parent Element Category:', 'text_domain' ),
'new_item_name' => __( 'New Element Category', 'text_domain' ),
'add_new_item' => __( 'Add Element Category', 'text_domain' ),
'edit_item' => __( 'Edit Element Category', 'text_domain' ),
'update_item' => __( 'Update Element Category', 'text_domain' ),
'view_item' => __( 'View Element Category', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => false,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'rewrite' => false,
'show_in_rest' => true,
);
register_taxonomy( 'element_category', array( 'gp_elements' ), $args );
}
add_action( 'init', 'Element_Category', 0 );
Yea, all I'm really after is the ability to filter/categorize in the admin. I'll give this a whirl on a new install and see what comes of it.
Thanks for the speedy reply David!
You're welcome - let us know how you get on.
It does work! 🙂
However, it requires a bit more code to get filtering in the admin/bulk editor to work. For anyone that's interested, here's the additional code needed...
/**
* Filter elements by taxonomy in admin
*/
function rbd_filter_elements_by_taxonomy() {
global $typenow;
$post_type = 'gp_elements';
$taxonomy = 'element_category';
if ($typenow == $post_type) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => sprintf( __( 'All %s' ), $info_taxonomy->label ),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
add_action('restrict_manage_posts', 'rbd_filter_elements_by_taxonomy');
function rbd_convert_id_to_term_in_query($query) {
global $pagenow;
$post_type = 'gp_elements';
$taxonomy = 'element_category';
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
add_filter('parse_query', 'rbd_convert_id_to_term_in_query');
Glad to hear that, and thanks for sharing your final code!