Support

Home Forums Event Espresso Premium Exclude category from sidebar widget filter

Exclude category from sidebar widget filter

Posted: March 19, 2024 at 10:33 am

Viewing 5 reply threads


MikeP

March 19, 2024 at 10:33 am

Is there a filter for excluding events from the side bar widget? Currently I have a sidebar widget on our homepage displaying all events. I recently created a category “private” I was successful in removing them from main events list using CSS but the widget doesn’t seem to have any selectors to use that same method. Any idea with this. I’ve read through the forum but it’s not making much sense to me.


Sam

  • Support Staff

March 19, 2024 at 11:25 am

Hi There,

It seems you are using the upcoming events widget on the website.

We recommend to check this ticket to remove a particular category from the sidebar:

https://eventespresso.com/topic/exclude-event-categories-from-upcoming-events-widget/

Hope that helps.


MikeP

March 19, 2024 at 1:42 pm

Hmm. Is there a filter example that works that I can just put in the category name or ID? More of an end user here trying to piece this all together. That thread is a bit confusing and the model made me even more confused. Thanks, any help appreciated.


MikeP

March 19, 2024 at 2:27 pm

TRying this but stull not excluding:

add_filter('FHEE__EEW_Upcoming_Events__widget__where', 'filter_exclude_category', 10, 3);
function filter_exclude_category($where, $category, $show_expired) {
    $category_to_exclude = 95;

    // Modify the condition to exclude events from the specified category
    $where['Term_Taxonomy.Term.term_id'] = array('!=', $category_to_exclude);

    return $where;
}


MikeP

March 21, 2024 at 11:26 am

I think I figured out part of it but still a bit unclear. I have a file called Upcoming_Events.php that seems to override the widget so I have made edits to that with the following code, where I am excluding category ID 95 “private” but I am still having an issue where if an event has no category assigned to it, it also wont show. I tried writing some more logic but just keep breaking the site. Any ideas? Otherwise this will do and I’ll just make sure we always have an event inside a category.. another way to solve this is to force a default category. hmm..

// grab widget settings
				$category = isset( $instance['category_name'] ) && ! empty( $instance['category_name'] ) ? $instance['category_name'] : FALSE;
				$show_expired = isset( $instance['show_expired'] ) ? (bool) absint( $instance['show_expired'] ) : FALSE;
				//$image_size = isset( $instance['image_size'] ) && ! empty( $instance['image_size'] ) ? $instance['image_size'] : 'medium';
				//$show_desc = isset( $instance['show_desc'] ) ? (bool) absint( $instance['show_desc'] ) : TRUE;
				$show_dates = isset( $instance['show_dates'] ) ? (bool) absint( $instance['show_dates'] ) : TRUE;
				$date_limit = isset( $instance['date_limit'] ) && ! empty( $instance['date_limit'] ) ? $instance['date_limit'] : NULL;
				$date_range = isset( $instance['date_range'] ) && ! empty( $instance['date_range'] ) ? $instance['date_range'] : FALSE;
				// start to build our where clause
				$where = array(
//					'Datetime.DTT_is_primary' => 1,
					'status' => 'publish'
				);
				// add category
                                
				if ( $category ) {
					$where['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
					$where['Term_Taxonomy.Term.slug'] = $category;
				}
   				 // add category exclusion condition
   					 $where['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
   					 $where['Term_Taxonomy.Term.term_id'] = array( '!=', 95 ); // Exclude events from category with ID 95 (change 95 to the desired category ID to be excluded)
				
				// if NOT expired then we want events that start today or in the future
				if ( ! $show_expired ) {
					$where['Datetime.DTT_EVT_end'] = array( '>=', EEM_Datetime::instance()->current_time_for_query( 'DTT_EVT_end' ) );
				}
                                //echo '<pre>';print_r($where);echo '</pre>';die;
				// run the query
				$events = EE_Registry::instance()->load_model( 'Event' )->get_all( array(
					$where,
					'limit' => $instance['limit'] > 0 ? '0,' . $instance['limit'] : '0,10',
					'order_by' => 'Datetime.DTT_EVT_start',
					'order' => 'ASC',
					'group_by' => 'EVT_ID'
				));
                                


Tony

  • Support Staff

March 21, 2024 at 2:20 pm

You don’t need to override the default Widget for this, you can get the same effect with the filter using:


add_filter('FHEE__EEW_Upcoming_Events__widget__where', 'filter_exclude_category', 10, 3);
function filter_exclude_category($where, $category, $show_expired) {
    $category_to_exclude = [95];

    // Modify the condition to exclude events from the specified category
    $where['Term_Taxonomy.taxonomy']  = 'espresso_event_categories';
    $where['Term_Taxonomy.Term.term_id'] = array('NOT IN', $category_to_exclude);

    return $where;
}

I am still having an issue where if an event has no category assigned to it, it also wont show..

This is the same issue I’ve been looking into before replying to this.

I think without the ability to run a tax_query (which you can’t do through the model system) then this is as good as it’s going to get.

However…. if using this method you always need to have a category assigned to the event right? So in that case what about using a ‘public’ category and selecting tha from within the widget. That removes the need for custom code here and just displays all of the events in that public category by explicitly selecting it in the widet settings.

Viewing 5 reply threads

The support post ‘Exclude category from sidebar widget filter’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso