{"id":1192744,"date":"2016-11-15T14:08:06","date_gmt":"2016-11-15T22:08:06","guid":{"rendered":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/"},"modified":"2016-11-15T14:08:06","modified_gmt":"2016-11-15T22:08:06","slug":"generic-function-for-adding-custom-taxonomy-to-the-filter-bar","status":"closed","type":"topic","link":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/","title":{"rendered":"Generic function for adding custom taxonomy to the filter bar"},"content":{"rendered":"<p>Hi,<\/p>\n<p>I had been searching around the forums and saw a bunch of custom implementations, and (thanks to that) using very little effort cobbled together a drop-in set of code (basically taking the category template and modifying a single variable twice) that you can use to easily add as many taxonomies as you want to the filter bar:<\/p>\n<pre><code>if (class_exists(&#039;Tribe__Events__Filterbar__Filter&#039;)):\n class Tribe__Events__Filterbar__Custom extends Tribe__Events__Filterbar__Filter {\n \tpublic $type = &#039;select&#039;;\n\n \tpublic function get_admin_form() {\n \t\t$title = $this-&gt;get_title_field();\n \t\t$type = $this-&gt;get_multichoice_type_field();\n \t\treturn $title.$type;\n \t}\n\n \tprotected function get_values() {\n \t\t$terms = array();\n\n \t\t\/\/ Load all available event categories\n \t\t$source = get_terms( $this-&gt;slug, array( &#039;orderby&#039; =&gt; &#039;name&#039;, &#039;order&#039; =&gt; &#039;ASC&#039; ) );\n \t\tif ( empty( $source ) || is_wp_error( $source ) ) return array();\n\n \t\t\/\/ Preprocess the terms\n \t\tforeach ( $source as $term ) {\n \t\t\t$terms[ (int) $term-&gt;term_id ] = $term;\n \t\t\t$term-&gt;parent = (int) $term-&gt;parent;\n \t\t\t$term-&gt;depth = 0;\n \t\t\t$term-&gt;children = array();\n \t\t}\n\n \t\t\/\/ Initally copy the source list of terms to our ordered list\n \t\t$ordered_terms = $terms;\n\n \t\t\/\/ Re-order!\n \t\tforeach ( $terms as $id =&gt; $term ) {\n \t\t\t\/\/ Skip root elements\n \t\t\tif ( 0 === $term-&gt;parent ) continue;\n\n \t\t\t\/\/ Reposition child terms within the ordered terms list\n \t\t\tunset( $ordered_terms[ $id ] );\n \t\t\t$term-&gt;depth = $terms[ $term-&gt;parent ]-&gt;depth + 1;\n \t\t\t$terms[ $term-&gt;parent ]-&gt;children[ $id ] = $term;\n \t\t}\n\n \t\t\/\/ Finally flatten out and return\n \t\treturn $this-&gt;flattened_term_list( $ordered_terms );\n \t}\n\n \t\/**\n \t * Flatten out the hierarchical list of event categories into a single list of values,\n \t * applying formatting (non-breaking spaces) to help indicate the depth of each nested\n \t * item.\n \t *\n \t * @param array $term_items\n \t * @param array $existing_list\n \t * @return array\n \t *\/\n \tprotected function flattened_term_list( array $term_items, array $existing_list = null ) {\n \t\t\/\/ Pull in the existing list when called recursively\n \t\t$flat_list = is_array( $existing_list ) ? $existing_list : array();\n\n \t\t\/\/ Add each item - including nested items - to the flattened list\n \t\tforeach ( $term_items as $term ) {\n \t\t\t$flat_list[] = array(\n \t\t\t\t&#039;name&#039;  =&gt; str_repeat( &#039;&amp;nbsp;&#039;, $term-&gt;depth * 2 ) . $term-&gt;name,\n \t\t\t\t&#039;value&#039; =&gt; $term-&gt;term_id,\n \t\t\t\t&#039;data&#039;  =&gt; array( &#039;slug&#039; =&gt; $term-&gt;slug ),\n \t\t\t\t&#039;class&#039; =&gt; &#039;tribe-events-category-&#039; . $term-&gt;slug,\n \t\t\t);\n\n \t\t\tif ( ! empty( $term-&gt;children ) ) {\n \t\t\t\t$child_items = $this-&gt;flattened_term_list( $term-&gt;children, $existing_list );\n \t\t\t\t$flat_list = array_merge( $flat_list, $child_items );\n \t\t\t}\n \t\t}\n\n \t\treturn $flat_list;\n \t}\n\n \t\/**\n \t * This method will only be called when the user has applied the filter (during the\n \t * tribe_events_pre_get_posts action) and sets up the taxonomy query, respecting any\n \t * other taxonomy queries that might already have been setup (whether by The Events\n \t * Calendar, another plugin or some custom code, etc).\n \t *\n \t * @see Tribe__Events__Filterbar__Filter::pre_get_posts()\n \t *\n \t * @param WP_Query $query\n \t *\/\n \tprotected function pre_get_posts( WP_Query $query ) {\n \t\t$new_rules      = array();\n \t\t$existing_rules = (array) $query-&gt;get( &#039;tax_query&#039; );\n \t\t$values         = (array) $this-&gt;currentValue;\n\n \t\t$new_rules[] = array(\n \t\t\t&#039;taxonomy&#039; =&gt; $this-&gt;slug,\n \t\t\t&#039;operator&#039; =&gt; &#039;IN&#039;,\n \t\t\t&#039;terms&#039;    =&gt; $values,\n \t\t);\n\n \t\t\/**\n \t\t * Controls the relationship between different taxonomy queries.\n \t\t *\n \t\t * If set to an empty value, then no attempt will be made by the additional field filter\n \t\t * to set the meta_query &quot;relation&quot; parameter.\n \t\t *\n \t\t * @var string $relation &quot;AND&quot;|&quot;OR&quot;\n \t\t *\/\n \t\t$relationship = apply_filters( &#039;tribe_events_filter_taxonomy_relationship&#039;, &#039;AND&#039; );\n\n \t\t\/**\n \t\t * If taxonomy filter meta queries should be nested and grouped together.\n \t\t *\n \t\t * The default is true in WordPress 4.1 and greater, which allows for greater flexibility\n \t\t * when combined with taxonomy queries added by other filters\/other plugins.\n \t\t *\n \t\t * @var bool $group\n \t\t *\/\n \t\t$nest = apply_filters( &#039;tribe_events_filter_nest_taxonomy_queries&#039;,\n \t\t\tversion_compare( $GLOBALS[&#039;wp_version&#039;], &#039;4.1&#039;, &#039;&gt;=&#039; )\n \t\t);\n\n \t\tif ( $nest ) {\n \t\t\t$new_rules = array(\n \t\t\t\t__CLASS__ =&gt; $new_rules,\n \t\t\t);\n \t\t}\n\n \t\t$tax_query = array_merge_recursive( $existing_rules, $new_rules );\n\n \t\t\/\/ Apply the relationship (we leave this late, or the recursive array merge would potentially cause duplicates)\n \t\tif ( ! empty( $relationship ) &amp;&amp; $nest ) {\n \t\t\t$tax_query[ __CLASS__ ][ &#039;relation&#039; ] = $relationship;\n \t\t} elseif ( ! empty( $relationship ) ) {\n \t\t\t$tax_query[ &#039;relation&#039; ] = $relationship;\n \t\t}\n\n \t\t\/\/ Apply our new meta query rules\n \t\t$query-&gt;set( &#039;tax_query&#039;, $tax_query );\n \t}\n }\nendif;<\/code><\/pre>\n<p>Every taxonomy you want to add is:<\/p>\n<p><code>new Tribe__Events__Filterbar__Custom(&#039;Taxonomy Name&#039;,&#039;tax_slug&#039;);<\/code><\/p>\n<p>I would advise putting these before the endif.<\/p>\n<p>That&#8217;s all. Hope this helps someone else.<\/p>\n","protected":false},"template":"","class_list":["post-1192744","topic","type-topic","status-closed","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Generic function for adding custom taxonomy to the filter bar -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Generic function for adding custom taxonomy to the filter bar -\" \/>\n<meta property=\"og:description\" content=\"Hi, I had been searching around the forums and saw a bunch of custom implementations, and (thanks to that) using very little effort cobbled together a drop-in set of code (basically taking the category template and modifying a single variable twice) that you can use to easily add as many taxonomies as you want to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/\",\"url\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/\",\"name\":\"Generic function for adding custom taxonomy to the filter bar -\",\"isPartOf\":{\"@id\":\"https:\/\/theeventscalendar.com\/support\/#website\"},\"datePublished\":\"2016-11-15T22:08:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/theeventscalendar.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Topics\",\"item\":\"https:\/\/theeventscalendar.com\/support\/topics\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Calendar Products\",\"item\":\"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Filter Bar\",\"item\":\"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/filter-bar\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Generic function for adding custom taxonomy to the filter bar\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/#website\",\"url\":\"https:\/\/theeventscalendar.com\/support\/\",\"name\":\"\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/theeventscalendar.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Generic function for adding custom taxonomy to the filter bar -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/","og_locale":"en_US","og_type":"article","og_title":"Generic function for adding custom taxonomy to the filter bar -","og_description":"Hi, I had been searching around the forums and saw a bunch of custom implementations, and (thanks to that) using very little effort cobbled together a drop-in set of code (basically taking the category template and modifying a single variable twice) that you can use to easily add as many taxonomies as you want to [&hellip;]","og_url":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/","url":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/","name":"Generic function for adding custom taxonomy to the filter bar -","isPartOf":{"@id":"https:\/\/theeventscalendar.com\/support\/#website"},"datePublished":"2016-11-15T22:08:06+00:00","breadcrumb":{"@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/generic-function-for-adding-custom-taxonomy-to-the-filter-bar\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/theeventscalendar.com\/support\/"},{"@type":"ListItem","position":2,"name":"Topics","item":"https:\/\/theeventscalendar.com\/support\/topics\/"},{"@type":"ListItem","position":3,"name":"Calendar Products","item":"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/"},{"@type":"ListItem","position":4,"name":"Filter Bar","item":"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/filter-bar\/"},{"@type":"ListItem","position":5,"name":"Generic function for adding custom taxonomy to the filter bar"}]},{"@type":"WebSite","@id":"https:\/\/theeventscalendar.com\/support\/#website","url":"https:\/\/theeventscalendar.com\/support\/","name":"","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/theeventscalendar.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1192744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1192744\/revisions"}],"wp:attachment":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/media?parent=1192744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}