Looping Over MEC Events

Hi,

I am trying to create a listing of upcoming MEC events at various locations using loopers, but am struggling to find the right solution.

On each date there may be up to four events at a specific location.

For example, Location A has events at 2pm and 3pm on 1st July and Location B has events at 6pm and 8pm on 1st July.

What I am trying to achieve is to create a list of each location which has an event and show its date.

Using the above example, the list would provide results:

  • Location A - 1st July
  • Location B - 1st July
  • Location X - ongoing dates

Are you able to suggest how I can achieve this using loopers, please?

Thanks,
Christopher

Hello Christopher,

Thanks for writing in! You can use the Looper Query Builder and add the custom fields for the date and the locations.

Kindly let us know how it goes.

Thanks @ruenel,

I now have the basics of what I was trying to achieved, helped also by the function in this post MEC Location Name.

However, there is one thing I have not been able to find the solution for. As originally mentioned, locations can have multiple events on a single day - usually two events, but sometimes 3 or 4 per day. Consequently, I get duplicate results (see screenshot):

How can I display the results without the duplicate entry for each date? Is this possible?

Thanks,
Christopher

Hi Christopher,

I have checked it through the Event List in WordPress admin and found that the 6th July 2024 has multiple events and 2 in each location. So this is not a duplicate entry, to avoid confussion you can show the Start Time and the End Time along with the date.

Hope it helps.
Thanks

Hi,

My point was that even though there are multiple events per location, I want to show just one entry from the looper in the output, to show which location has events on a single date.

Thanks

Hello @whitemedia,

Be advised that the Looper will return all items that align with the meta field condition. This is the reason why you are having duplicates. You will need a custom PHP function to be used in the Looper Provider Custom that eliminates two or more similar events under the same location.

Be advised that custom PHP coding is beyond the scope of our support under our Support Policy. If you are unfamiliar with code and resolving potential conflicts, you may select our One service for further assistance.

Best Regards.

Thanks for the pointer to custom looper provider @ruenel. I have found a solution to what I wanted.

//============================================================
// CUSTOM LOOPER PROVIDER FUNCTION TO GET UNIQUE EVENTS / DATES
//============================================================
function custom_mec_events_query() {
    $current_date = date('Y-m-d');
    $event_category_ids = [53, 54, 55];
    $location_ids = [56, 57, 43];

    $args = array(
        'post_type' => 'mec-events',
        'post_status' => 'publish',
        'meta_key' => 'mec_start_date',
        'meta_query' => array(
            array(
                'key' => 'mec_start_date',
                'value' => $current_date,
                'compare' => '>=',
                'type' => 'DATE',
            ),
        ),
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'mec_category',
                'field' => 'term_id',
                'terms' => $event_category_ids,
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'mec_location',
                'field' => 'term_id',
                'terms' => $location_ids,
                'operator' => 'IN',
            ),
        ),
        'orderby' => array(
            'meta_value' => 'ASC', // Meta key sorting should reference 'meta_value'
            'title' => 'ASC', // Sort by title as fallback
        ),
        'posts_per_page' => 1000, // Increase number of results
    );

    $query = new WP_Query($args);

    if ($query->have_posts()) {
        $unique_events = [];
        $seen_pairs = [];

        foreach ($query->posts as $post) {
            $event_start_date = get_post_meta($post->ID, 'mec_start_date', true);
            $location_terms = get_the_terms($post->ID, 'mec_location');
            $location_name = $location_terms && !is_wp_error($location_terms) ? join(', ', wp_list_pluck($location_terms, 'name')) : '';

            $pair = $event_start_date . '|' . $location_name;
            if (!in_array($pair, $seen_pairs)) {
                $seen_pairs[] = $pair;

                $unique_events[] = array(
                    'event_id' => $post->ID,
                    'event_title' => $post->post_title,
                    'event_start_date' => $event_start_date,
                    'location_name' => strip_tags($location_name),
                );
            }
        }

        wp_reset_postdata(); // Reset post data here

        return $unique_events;
    } else {
        // Handle no events found scenario (optional)
        return array();
    }
}

// Register the custom Looper Provider
add_filter('cs_looper_custom_custom_mec_events', function($result) {
    return custom_mec_events_query();
});

Looper Provider type: Custom
Hook: custom_mec_events

Looper Consumer Fields (note this is using Dynamic Content 2.0 in Pro 6.5.0 Beta 4)
{{ looper.field({"key":"event_start_date"})|date('l, jS F Y') }}
{{ looper.field({"key":"location_name"}) }}

This all provides unique date / location events where there may be multiple events on a day in a location.

Hi Christopher,

Glad that you are able to find the solution to it, and share it with others.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.