Support

Home Forums Event Espresso Premium Filtering Events by Multiple Categories

Filtering Events by Multiple Categories

Posted: September 7, 2024 at 12:46 am

Viewing 5 reply threads


Mark

September 7, 2024 at 12:46 am

I am using event espresso 5 , I want to show events of only 2 specific categories, how can I use. category_slugs which I want to combine with AND operator are granville-richmond,efa . I tried to use [ESPRESSO_EVENTS category_slug=”efa,granville-richmond”] but I think its enlisting with OR operator.
Just to rephrase, I want to enlist only those events which falls in category1 AND in categories2. Is there any default method to use if not can you pls help how can I do it?
Thank you so much.


Tony

  • Support Staff

September 9, 2024 at 7:52 am

Hi Mark,

We don’t have a built-in shortcode method for the above, but you can do it with your own WP_Query and tax_query and there is a built-in EventListQuery class that extends WP_Query.

So something like thi:


//set default params
$events_params = [
	'limit'         => 10,
	'show_expired'  => false,
	'month'         => null,
	'order_by'      => 'start_date',
	'sort'          => 'ASC',
	'tax_query' => [
			[
			'taxonomy'  => 'espresso_event_categories',
			'field'     => 'slug', //change to name or slug if necessary
			'terms'     => ['one-category', 'two-category'],
			'operator'  => 'AND'
	   ]
	],
];

$events_query = new EventEspresso\core\domain\services\wp_queries\EventListQuery($events_params);
// assign results to a variable so we can return it
$events = $events_query->have_posts() ? $events_query->posts : [];
// but first reset the query and postdata
wp_reset_query();
wp_reset_postdata();

Would return an array of events that were in one-category AND two-category.

The above function is also used within our template_tag function espresso_get_events($params = []).

Is that what you are looking for?


Mark

September 14, 2024 at 4:44 am

I’ve successfully implemented the provided code to filter and display events based on categories, and the event data is being shown correctly. However, I’m encountering an issue with the event addresses.

I’m unable to determine the exact cause of the issue with the event addresses. Could you please review the following code and provide guidance on how to resolve it:

/*
* Shortcode Function to filter and show events based on categories
*/
add_shortcode(‘fds_espresso_events’, ‘fds_custom_espresso_events_shortcode’);
function fds_custom_espresso_events_shortcode($atts) {
// Extract shortcode attributes
$atts = shortcode_atts(
array(
‘category_slug’ => ”, // Comma-separated list of category slugs
),
$atts,
‘espresso_events’
);

// Get category slugs
$category_slugs = array_map(‘trim’, explode(‘,’, $atts[‘category_slug’]));

// Check if category slugs are provided
if (empty($category_slugs)) {
return ‘Please provide category slugs.’;
}

// Set default params
$events_params = [
‘limit’ => -1,
‘show_expired’ => false,
‘month’ => null,
‘order_by’ => ‘start_date’,
‘sort’ => ‘ASC’,
‘tax_query’ => [
[
‘taxonomy’ => ‘espresso_event_categories’,
‘field’ => ‘slug’, //change to name or slug if necessary
‘terms’ => $category_slugs,
‘operator’ => ‘AND’
]
],
];

// Execute the query
$events_query = new EventEspresso\core\domain\services\wp_queries\EventListQuery($events_params);
$events = $events_query->have_posts() ? $events_query->posts : [];

// Reset query and postdata
wp_reset_query();
wp_reset_postdata();

// Start output buffer
ob_start();
// Custom title for events list
echo ‘<h1 class=”fds-events-page-title”>Upcoming Events</h1>’;
if (!empty($events)) {
foreach ($events as $event) {
// Event class (optional, modify as needed)
$event_class = has_excerpt($event->ID) ? ‘ has-excerpt’ : ”;
$event_class = apply_filters(‘FHEE__content_espresso_events__event_class’, $event_class);
?>
<article id=”post-<?php echo $event->ID; ?>” <?php post_class($event_class); ?> style=”margin-bottom: 30px;”>
<div id=”espresso-event-list-header-dv-<?php echo $event->ID; ?>” class=”espresso-event-header-dv”>
<?php
if (function_exists(‘espresso_get_template_part’)) {
espresso_get_template_part(‘content’, ‘espresso_events-thumbnail’);
// Remove or comment out the header part if it’s causing duplication
// espresso_get_template_part(‘content’, ‘espresso_events-header’);
}
?>
</div>

<div class=”espresso-event-list-wrapper-dv”>
<div id=”event_data-<?php echo $event->ID; ?>”>
<div class=”event_data_display event-data-display ui-widget-content ui-corner-bottom”>
<h2 class=”event-title”><?php echo $event->post_title; ?></h2>
<div class=”event-desc”>
<div>
<b>Price:</b> <?php
if (function_exists(‘espresso_event_tickets_available’)) {
echo espresso_event_tickets_available($event->ID);
} else {
echo ‘Price information not available.’;
}
?>
</div>
<div>
<b>Date & Time: </b> <?php
if (function_exists(‘espresso_list_of_event_dates’)) {
echo espresso_list_of_event_dates($event->ID);
} else {
echo ‘Date information not available.’;
}
?>
</div>
<div class=”address”>
<b>Address: </b> <?php
if (function_exists(‘espresso_venue_address’)) {
echo espresso_venue_address($event->ID);
} else {
echo ‘Address information not available.’;
}
?>
</div>
<div>
<b>Available Space: </b> <?php
if (class_exists(‘EEH_Event_View’) && method_exists(‘EEH_Event_View’, ‘get_event’)) {
$event_obj = EEH_Event_View::get_event($event->ID);
echo $event_obj->spaces_remaining_for_sale();
} else {
echo ‘Available space information not available.’;
}
?>
</div>
</div>
<div id=”register_link-<?php echo $event->ID; ?>”>
ID; ?>” href=”<?php echo get_permalink($event->ID); ?>”><?php _e(‘Register’, ‘event_espresso’); ?>
</div>
</div>
</div>
</div>

</article>
<?php
}
} else {
echo ‘No events found for the specified categories.’;
}

// Return output
return ob_get_clean();
}

You can view the page where the issue is occurring here: https://vancouverfirstaid.ca/testing-slugs/

Please find a screenshot of the issue here: https://app.screencast.com/4E6mp2td68X3I


Tony

  • Support Staff

September 16, 2024 at 5:23 am

The signature for the espresso_venue_address() function is:

espresso_venue_address($type = 'multiline', $VNU_ID = 0, $echo = true)

So you need to pass it the Venue ID assigned to the event for it to work. For example:

espresso_venue_address('multiline', $VNU_ID)

But, you don’t have the Venue ID there.

As you are using EventListQuery each post should already have the EE_Event object set on it

$event->EE_Event

So do something like this as you enter your events loop:

foreach($events as $event) {
    $event = isset($event->EE_Event) ? $event->EE_Event : $event;

    //Use $event as an EE_event object from this point.
    
}

$event will continue to work as it does currently but now its one of our models which has a bunch of additional method available.

Such as $venue = $event->venue();

If you have a venue assigned to the event the above returns an EE_Venue object, which again has a bunch of additional methods available to get whatever data you need.


Mark

September 18, 2024 at 1:17 am

Thanks! Your assistance is greatly appreciated. I’ve implemented your suggestions and the event venue address is now displaying correctly.
https://app.screencast.com/et5w8eZxkLbkK


Tony

  • Support Staff

September 18, 2024 at 2:08 am

Awesome, glad it helped 🙂

Viewing 5 reply threads

The support post ‘Filtering Events by Multiple Categories’ 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