Home › Forums › Calendar Products › Filter Bar › ACF and filtering
- This topic has 4 replies, 2 voices, and was last updated 6 years, 8 months ago by Pascha Apter.
-
AuthorPosts
-
August 17, 2017 at 7:54 am #1336779Pascha ApterParticipant
I have created a custom post type for ‘performers’. I have created a custom field for events using ACF which is a post object populated with the performers. I would like to add a filter to the filter bar to filter events by this custom post type ‘performer’ in the same way you can filter by ‘venue’ or ‘organizer’. Any direction on how I could accomplish this?
August 17, 2017 at 8:31 am #1336805Pascha ApterParticipantI have attempted creating a custom filter for the filter bar using this tutorial:
https://theeventscalendar.com/knowledgebase/creating-custom-filters-for-filter-bar/
But I keep getting this error:
Fatal error: Class ‘Tribe__Events__Filterbar__Filter’ not found
I have added the new class and the call to instantiate the new class into a custom plugin file.
Here is the code:
class Tribe__Events__Filterbar__Filters__Performer extends Tribe__Events__Filterbar__Filter { public $type = 'checkbox'; public function get_admin_form() { $title = $this->get_title_field(); $type = $this->get_multichoice_type_field(); return $title.$type; } protected function get_values() { /** @var wpdb $wpdb */ global $wpdb; // get performer IDs associated with published posts $performer_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT m.meta_value FROM {$wpdb->postmeta} m INNER JOIN {$wpdb->posts} p ON p.ID=m.post_id WHERE p.post_type=%s AND p.post_status='publish' AND m.meta_key='performer' AND m.meta_value > 0", Tribe__Events__Main::POSTTYPE ) ); array_filter( $performer_ids ); if ( empty( $performer_ids ) ) { return array(); } /** * Filter Total Performers in Filter Bar * Use this with caution, this will load performers on the front-end, may be slow * The base limit is 200 for safety reasons * * * @parm int 200 posts per page limit * @parm array $performer_ids ids of performers attached to events */ $limit = apply_filters( 'tribe_events_filter_bar_performers_limit', 200, $performer_ids ); $performers = get_posts( array( 'post_type' => 'performer', 'posts_per_page' => $limit, 'suppress_filters' => false, 'post__in' => $performer_ids, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', ) ); $performers_array = array(); foreach ( $performers as $performer ) { $performers_array[] = array( 'name' => $performer->post_title, 'value' => $performer->ID, ); } return $performers_array; } protected function setup_join_clause() { global $wpdb; $this->joinClause = "LEFT JOIN {$wpdb->postmeta} AS performer_filter ON ({$wpdb->posts}.ID = performer_filter.post_id AND performer_filter.meta_key = 'performer')"; } protected function setup_where_clause() { if ( is_array( $this->currentValue ) ) { $performer_ids = implode( ',', array_map( 'intval', $this->currentValue ) ); } else { $performer_ids = esc_attr( $this->currentValue ); } $this->whereClause = " AND performer_filter.meta_value IN ($performer_ids) "; } } new Tribe__Events__Filterbar__Filters__Performer( __( 'Performer', 'tribe-events-filter-view' ), 'performer' );
August 17, 2017 at 9:10 am #1336830Pascha ApterParticipantI think I have solved this by moving the code out of the plugin file and into my functions.php file.
August 17, 2017 at 9:44 am #1336924NicoMemberHi there Leif,
Thanks for reaching out to us and for the heads-up on the resolution of the issue 🙂
What you did is correct, the custom code should live in the theme (or child theme) functions file or in a customizations plugin for the site (more information here).
I’ll go ahead and close out this thread, but if you need help with anything else please don’t hesitate to create a new one and we will be happy to assist you.
Best,
Nico -
AuthorPosts
- The topic ‘ACF and filtering’ is closed to new replies.