Mmcustombanner

Geodirectory Events Shortcode

|

|

The following Geodirectory Events Shortcode can be utilized either as a plugin (saved as a php file) or utilized as a snippet using the Code Snippets plugin found in the WordPress Repository.

Additionally, the Geodirectory Register Post Types plugin is a must-run plugin and thus is required to be saved as a php file in the mu-plugin folder in WordPress.

The shortcode is meant to be used with a Multisite setup that has the Geodirectory plugin installed with the events addon.

Here’s how it looks in use:

Screenshot 2025 07 19 143419

Here’s how it looks when being applied on a shortcode:

Screenshot 2025 07 19 143456

You’d use the plugin in the following way with the WordPress shortcode block on a page or post:

[geo_gd_events site_id="1" count="5" category_slug="category" tag_slug="tag" show_image="true" title_tag="h5"]

The shortcode was coded by ChatGPT and tested internally before being released.

/**
 * Shortcode: [geo_gd_events]
 * Description: Display upcoming GeoDirectory Events from another site in a WordPress Multisite.
 * Author: Your Name or Project
 * License: GPLv2 or later
 */

add_shortcode('geo_gd_events', 'geo_shortcode_gd_events');
function geo_shortcode_gd_events($atts) {
    $atts = shortcode_atts(array(
        'site_id' => 1,
        'display' => 10,
        'category_slug' => '',
        'tag_slug' => '',
        'show_image' => 'true',
        'image_size' => 'medium',
        'title_tag' => 'h5',
        'title_position' => 'below',
        'random' => 'false',
        'debug' => 'false'
    ), $atts, 'geo_gd_events');

    $site_id = (int) $atts['site_id'];
    $display = (int) $atts['display'];
    $category_slug = sanitize_text_field($atts['category_slug']);
    $tag_slug = sanitize_text_field($atts['tag_slug']);
    $show_image = $atts['show_image'] === 'true';
    $image_size = sanitize_text_field($atts['image_size']);
    $title_tag = tag_escape($atts['title_tag']);
    $title_position = $atts['title_position'] === 'above' ? 'above' : 'below';
    $random = $atts['random'] === 'true';
    $debug = $atts['debug'] === 'true';

    switch_to_blog($site_id);
    global $wpdb;

    $today = current_time('Y-m-d');

    // Step 1: Get future event_ids
    $future_event_ids = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT DISTINCT event_id FROM {$wpdb->prefix}geodir_event_schedule WHERE start_date >= %s ORDER BY start_date ASC",
            $today
        )
    );

    if (empty($future_event_ids)) {
        restore_current_blog();
        return '<p>No upcoming events found.</p>';
    }

    // Step 2: WP_Query for matching events
    $args = array(
        'post_type' => 'gd_event',
        'post_status' => 'publish',
        'posts_per_page' => $display,
        'post__in' => $future_event_ids,
        'orderby' => $random ? 'rand' : 'post__in',
    );

    // Step 3: Taxonomy filtering
    $tax_query = array('relation' => 'AND');

    if ($category_slug) {
        $cat_slugs = array_map('trim', explode(',', $category_slug));
        $cat_ids = array();
        foreach ($cat_slugs as $slug) {
            $term = get_term_by('slug', $slug, 'gd_event_category');
            if ($term) $cat_ids[] = $term->term_id;
        }
        if (!empty($cat_ids)) {
            $tax_query[] = array(
                'taxonomy' => 'gd_event_category',
                'field' => 'term_id',
                'terms' => $cat_ids,
                'operator' => 'IN',
            );
        }
    }

    if ($tag_slug) {
        $tag_slugs = array_map('trim', explode(',', $tag_slug));
        $tag_ids = array();
        foreach ($tag_slugs as $slug) {
            $term = get_term_by('slug', $slug, 'gd_event_tags');
            if ($term) $tag_ids[] = $term->term_id;
        }
        if (!empty($tag_ids)) {
            $tax_query[] = array(
                'taxonomy' => 'gd_event_tags',
                'field' => 'term_id',
                'terms' => $tag_ids,
                'operator' => 'IN',
            );
        }
    }

    if (count($tax_query) > 1) {
        $args['tax_query'] = $tax_query;
    }

    $query = new WP_Query($args);

    if ($debug) {
        echo '<pre>' . print_r($args, true) . '</pre>';
    }

    if (!$query->have_posts()) {
        restore_current_blog();
        return '<p>No upcoming events found.</p>';
    }

    ob_start();
    echo '<ul class="geo-gd-events-list" style="list-style: none; padding-left: 0;">';

    while ($query->have_posts()) {
        $query->the_post();
        $post_id = get_the_ID();
        $title = get_the_title();
        $link = get_permalink();
        $thumb = get_the_post_thumbnail($post_id, $image_size);

        // Fetch event date from geodir_event_schedule
        $schedule = $wpdb->get_row(
            $wpdb->prepare(
                "SELECT start_date, start_time FROM {$wpdb->prefix}geodir_event_schedule WHERE event_id = %d ORDER BY start_date ASC LIMIT 1",
                $post_id
            )
        );

        $date_display = '';
        if ($schedule) {
            $date_obj = date_create_from_format('Y-m-d', $schedule->start_date);
            if ($date_obj) {
                $formatted_date = date_format($date_obj, 'F j, Y');
                $formatted_time = !empty($schedule->start_time) ? date('g:i A', strtotime($schedule->start_time)) : '';
                $date_display = $formatted_date . ($formatted_time ? ' at ' . $formatted_time : '');
            }
        }

        echo '<li class="geo-gd-event" style="margin-bottom: 1.5em;">';

        if ($title_position === 'above') {
            echo "<{$title_tag}><a href='{$link}'>{$title}</a></{$title_tag}>";
        }

        if ($show_image && $thumb) {
            echo "<div class='geo-gd-event-thumb' style='margin-bottom:0.5em;'><a href='{$link}'>{$thumb}</a></div>";
        }

        if ($title_position === 'below') {
            echo "<{$title_tag} style='margin: 0 0 0.3em 0;'><a href='{$link}'>{$title}</a></{$title_tag}>";
        }

        if ($date_display) {
            echo "<div class='geo-gd-event-date' style='color: #666; font-size: 0.9em;'>{$date_display}</div>";
        }

        echo '</li>';
    }

    echo '</ul>';
    wp_reset_postdata();
    restore_current_blog();

    return ob_get_clean();
}

Want to contribute back to its development with changes or improvements? Make sure to email them to info@myrickmultimedia.com to have them shared with everyone.


631769449 18008452514834461 5640658391828836863 n

TOP SHOTS: Spartans hit bullseye with undefeated season

Talk about hitting center of the target. These local teens…
Read More →
From Today Sports Wire

Haralson county sheriffs office

Haralson Jail report – Tuesday, February 10, 2026

Officials from the Haralson County Detention Center provide arrest reports…
Read More →
From Haralson Today

Police

REPORT: Admission leads to investigation into inappropriate material on lost phone

A Bremen man remained in custody on a felony charge…
Read More →
From Haralson Today

Myrick Multimedia
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.