Advertisement
Mmcustombanner
Wordpress

Geodirectory Places Shortcode

The following shortcode is a generic version of a customized snippet/plugin created with the use of ChatGPT to display any kind of listing that has been created using the Geodirectory plugin for WordPress, being used on a multisite installation.

This screenshot is an example of how it is used:

Screenshot 2025 07 19 143425

This is an example of the shortcode in use:

Screenshot 2025 07 19 144058

It can through the shortcode block display a title with image that links back to the original “listing” created on a main site to a network of sites across a WordPress Multisite installation. The custom version of this is providing classified listing display on several sites and has been tested thoroughly before being released.

Advertisement

This shortcode needs the Register Post Types for Geodirectory must-use plugin.

This snippet was activated using the Code Snippets plugin.

Here’s an example of how it is used:

Advertisement
[geo_gd_places site_id="1" show_image="true" image_size="medium" title_tag="h4"]

Here’s the code:

/**
 * [geo_gd_places] shortcode for displaying GeoDirectory Listings.
 * GPL-compatible public version with filtering and layout options.
 */
function geo_gd_places_shortcode($atts) {
    $atts = shortcode_atts([
        'site_id'       => get_current_blog_id(),
        'display'       => '5',
        'category_slug' => '',
        'tag_slug'      => '',
        'show_image'    => 'true',
        'image_size'    => 'medium',
        'title_tag'     => 'h5',
        'title_position'=> 'above',
        'random'        => 'false',
    ], $atts, 'geo_gd_places');

    switch_to_blog($atts['site_id']);

    $args = [
        'post_type'      => 'gd_place',
        'posts_per_page' => intval($atts['display']),
        'post_status'    => 'publish',
        'orderby'        => $atts['random'] === 'true' ? 'rand' : 'date',
        'order'          => 'DESC',
        'tax_query'      => ['relation' => 'AND']
    ];

    // Category filtering
    if ( $atts['category_slug'] ) {
        $cat_slugs = array_map('trim', explode(',', $atts['category_slug']));
        $cat_ids = [];
        foreach ($cat_slugs as $slug) {
            $term = get_term_by('slug', $slug, 'gd_placecategory');
            if ($term) {
                $cat_ids[] = intval($term->term_id);
            }
        }
        if ($cat_ids) {
            $args['tax_query'][] = [
                'taxonomy' => 'gd_placecategory',
                'field'    => 'term_id',
                'terms'    => $cat_ids,
            ];
        }
    }

    // Tag filtering
    if ( $atts['tag_slug'] ) {
        $tag_slugs = array_map('trim', explode(',', $atts['tag_slug']));
        $tag_ids = [];
        foreach ($tag_slugs as $slug) {
            $term = get_term_by('slug', $slug, 'gd_placetags');
            if ($term) {
                $tag_ids[] = intval($term->term_id);
            }
        }
        if ($tag_ids) {
            $args['tax_query'][] = [
                'taxonomy' => 'gd_placetags',
                'field'    => 'term_id',
                'terms'    => $tag_ids,
            ];
        }
    }

    $query = new WP_Query($args);
    $output = '<div class="geo-place-list">';
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $output .= '<div class="geo-place-item">';

            if ($atts['title_position'] === 'above') {
                $output .= '<' . $atts['title_tag'] . ' class="geo-place-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></' . $atts['title_tag'] . '>';
            }

            if ($atts['show_image'] === 'true' && has_post_thumbnail()) {
                $output .= get_the_post_thumbnail(get_the_ID(), $atts['image_size'], ['class' => 'geo-place-image']);
            }

            if ($atts['title_position'] === 'below') {
                $output .= '<' . $atts['title_tag'] . ' class="geo-place-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></' . $atts['title_tag'] . '>';
            }

            $output .= '</div>';
        }
        wp_reset_postdata();
    } else {
        $output .= '<p>No listings found.</p>';
    }

    $output .= '</div>';
    restore_current_blog();
    return $output;
}
add_shortcode('geo_gd_places', 'geo_gd_places_shortcode');

Want to contribute back to the development of this plugin? Make sure to email it to info@myrickmultimedia.com so it can be shared with everyone who wants to use it!

Advertisement

← Previous Register Post Types on WordPress Multisite for Geodirectory Next → Geodirectory Events Shortcode
+
×