- This topic has 5 replies, 2 voices, and was last updated 3 years, 4 months ago by
David.
-
AuthorPosts
-
December 14, 2022 at 11:04 am #2461981
Randy
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_elementspost 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_argsfilter? 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!
December 15, 2022 at 5:21 am #2462696David
StaffCustomer SupportHi there,
i am not sure of the ramifications of doing this, but you can simply
register_taxonomyfor thegp_elementspost 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 );December 15, 2022 at 9:17 am #2463123Randy
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!
December 16, 2022 at 2:19 am #2463811David
StaffCustomer SupportYou’re welcome – let us know how you get on.
December 16, 2022 at 9:52 am #2464431Randy
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');December 17, 2022 at 5:01 am #2464974David
StaffCustomer SupportGlad to hear that, and thanks for sharing your final code!
-
AuthorPosts
- You must be logged in to reply to this topic.