I have to be upfront and say I haven’t coded in around 10 years. My knowledge is close to zero coding PHP but being a IT professional I am not a “complete” idiot.
In the product list of Woocommerce (backend) I want to have a Brand filter. I made a product attribute called “Brand”. With the help of a few websites I compiled this bit of code in my Functions.php
add_filter( 'woocommerce_product_filters', 'Filter_chosen_attributes' ); function Filter_chosen_attributes( $output ) { global $wp_query; $output .= wc_product_dropdown_categories( array( 'show_option_none' => 'Filter by product attributes', 'taxonomy' => 'product_attributes', 'name' => 'product_attributes', 'selected' => isset( $wp_query->chosen_attributes['product_attributes'] ) ? $wp_query->chosen_attributes['product_attributes'] : '', ) ); return $output; }
I don’t know how to specify that I want to filter the brand product attribute.
Can anybody help?
Advertisement
Answer
based on this tutorial you could use something like this (place the code inside functions.php)
/** * Display a custom taxonomy dropdown in admin * @author Mike Hemberger * @link http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/ */ add_action('restrict_manage_posts', 'tsm_filter_post_type_by_taxonomy'); function tsm_filter_post_type_by_taxonomy() { global $typenow; $post_type = 'product'; // change to your post type $taxonomy = 'product_brand'; // change to your taxonomy if ($typenow == $post_type) { $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; $info_taxonomy = get_taxonomy($taxonomy); wp_dropdown_categories(array( 'show_option_all' => sprintf( __( 'Show all %s', 'textdomain' ), $info_taxonomy->label ), 'taxonomy' => $taxonomy, 'name' => $taxonomy, 'orderby' => 'name', 'selected' => $selected, 'show_count' => true, 'hide_empty' => true, )); }; } /** * Filter posts by taxonomy in admin * @author Mike Hemberger * @link http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/ */ add_filter('parse_query', 'tsm_convert_id_to_term_in_query'); function tsm_convert_id_to_term_in_query($query) { global $pagenow; $post_type = 'product'; // change to your post type $taxonomy = 'product_brand'; // change to your taxonomy $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; } }
to find the correct Taxonomy you have to look in the URL for the GET parameter taxonomy see example below the bolt part is the correct taxonomy
yourdomain.com/wp-admin/edit-tags.php?taxonomy=product_brand&post_type=product