Function Reference: geodir_sc_gd_listings

Summary

The geodirectory listings shortcode.

Description

This implements the functionality of the shortcode for displaying geodirectory listings.

Global Values

$post
(object) (required) The current post object.

Default: None

Parameters

$atts
(array) (required) {
Attributes of the shortcode. @type string $title The title to be displayed above listings. @type string $post_type Post type of listing. @type string $category Category ids to filter listings. Ex: 1,3. @type string $list_sort Sorting for listings. Should be from az, latest, featured,
high_review, high_rating. @type string $event_type Event type filter. Should today, upcoming, past, all. For post type gd_event only. @type int $post_number No. of post to display. @type int|string $post_author Filter the posts by author. Either author ID or ‘current'(it uses
the author ID of the current post. @type string $layout Layout to display listing. Should be gridview_onehalf, gridview_onethird
gridview_onefourth, gridview_onefifth, list. @type string $listing_width Listing width. Optional
@type int $character_count The excerpt length of content
@type int|bool $add_location_filter Filter listings using current location. @type int|bool $show_featured_only Display only featured listings. @type int|bool $show_special_only Display only special offers listings. @type int|bool $with_pics_only Display listings which has image available. @type int|bool $with_videos_only Display listings which has video available. @type int|bool $with_pagination Display pagination for listings. @type int|bool $top_pagination Display pagination on top of listings. Required $with_pagination true. @type int|bool $bottom_pagination Display pagination on bottom of listings. Required $with_pagination true.

Default: 1
$content
(string) (required) The enclosed content. Optional.

Default: None

Return Values

(string)
  • HTML content to display geodirectory listings.

Change Log

Since: 1.4.2

1.5.9 New parameter “post_author” added.

1.6.5 tags parameter added.

1.6.18 New attributes added in gd_listings shortcode to filter user favorite listings.
In [gd_listings] shortcode if category has no posts then it shows all the results – FIXED

Type

string $tags Post tags. Ex: “Tag1,TagB” Optional.
@type int|bool $show_favorites_only Display listings which are favorited by user. Default empty.
@type int|string $favorites_by_user Filter the posts favorites by user. Should be user ID or ‘current’ or empty. Default empty.
(‘current’ uses the author Id of current viewing post, If empty then uses the current logged user ID).
}

Source File

geodir_sc_gd_listings() is located in geodirectory_shortcodes.php [Line: 1225]

Source Code

function geodir_sc_gd_listings($atts, $content = '') {
    global $post;
    $defaults = array(
        'title'                 => '',
        'post_type'             => 'gd_place',
        'category'              => 0,
        'list_sort'             => 'latest',
        'event_type'            => '',
        'post_number'           => 10,
        'post_author'           => '',
        'layout'                => 'gridview_onehalf',
        'listing_width'         => '',
        'character_count'       => 20,
        'add_location_filter'   => 1,
        'show_featured_only'    => '',
        'show_special_only'     => '',
        'with_pics_only'        => '',
        'with_videos_only'      => '',
        'with_pagination'       => '1',
        'top_pagination'        => '0',
        'bottom_pagination'     => '1',
        'without_no_results'    => 0,
        'tags'                  => '',
        'show_favorites_only'   => '',
        'favorites_by_user'     => '',
    );
    $params = shortcode_atts($defaults, $atts);

    $params['title']        = wp_strip_all_tags($params['title']);
    $params['post_type']    = gdsc_is_post_type_valid($params['post_type']) ? $params['post_type'] : 'gd_place';

    // Validate the selected category/ies - Grab the current list based on post_type
    $category_taxonomy      = geodir_get_taxonomies($params['post_type']);
    $categories             = get_terms($category_taxonomy, array('orderby' => 'count', 'order' => 'DESC', 'fields' => 'ids', 'hide_empty' => 0));

    // Make sure we have an array
    if (!(is_array($params['category']))) {
        $params['category'] = explode(',', $params['category']);
    }

    // Array_intersect returns only the items in $params['category'] that are also in our category list
    // Otherwise it becomes empty and later on that will mean "All"
    $params['category']     = array_intersect($params['category'], $categories);

    // Post_number needs to be a positive integer
    $params['post_number']  = absint($params['post_number']);
    $params['post_number']  = $params['post_number'] > 0 ? $params['post_number'] : 10;
    
    // Post_number needs to be a positive integer
    if (!empty($params['post_author'])) {
        // 'current' left for backwards compatibility
        if ( $params['post_author'] == 'current' || $params['post_author'] == 'current_author') {
            if (!empty($post) && $post->post_type != 'page' && isset($post->post_author)) {
                $params['post_author'] = $post->post_author;
            } else {
                $params['post_author'] = -1; // Don't show any listings.
            }
        } else if ($params['post_author'] == 'current_user' ) {
            if ($current_user_id = get_current_user_id()) {
                $params['post_author'] = $current_user_id;
            } else {
                $params['post_author'] = -1; // If not logged in then don't show any listings.
            }
        } else if (absint($params['post_author']) > 0) {
            $params['post_author'] = absint($params['post_author']);
        } else {
            $params['post_author'] = -1; // Don't show any listings.
        }
    } else {
        unset($params['post_author']);
    }

    // Validate character_count
    //todo: is this necessary?
    $params['character_count']  = $params['character_count'];

    // Validate our layout choice
    // Outside of the norm, I added some more simple terms to match the existing
    // So now I just run the switch to set it properly.
    $params['layout']           = gdsc_validate_layout_choice($params['layout']);

    // Validate our sorting choice
    $params['list_sort']        = gdsc_validate_sort_choice($params['list_sort'], $params['post_type']);

    // Validate Listing width, used in the template widget-listing-listview.php
    // The context is in width=$listing_width% - So we need a positive number between 0 & 100
    $params['listing_width']    = gdsc_validate_listing_width($params['listing_width']);

    // Validate the checkboxes used on the widget
    $params['add_location_filter']  = gdsc_to_bool_val($params['add_location_filter']);
    $params['show_featured_only']   = gdsc_to_bool_val($params['show_featured_only']);
    $params['show_special_only']    = gdsc_to_bool_val($params['show_special_only']);
    $params['with_pics_only']       = gdsc_to_bool_val($params['with_pics_only']);
    $params['with_videos_only']     = gdsc_to_bool_val($params['with_videos_only']);
    $params['with_pagination']      = gdsc_to_bool_val($params['with_pagination']);
    $params['top_pagination']       = gdsc_to_bool_val($params['top_pagination']);
    $params['bottom_pagination']    = gdsc_to_bool_val($params['bottom_pagination']);
    
    // User favorites
    $params['show_favorites_only']  = gdsc_to_bool_val($params['show_favorites_only']);
    if (!empty($params['show_favorites_only'])) {
        if ( $params['favorites_by_user'] == 'current' || $params['favorites_by_user'] == 'current_author') {
            if (!empty($post) && $post->post_type != 'page' && isset($post->post_author)) {
                $params['favorites_by_user'] = $post->post_author;
            } else {
                $params['favorites_by_user'] = 0;
            }
        } else if ($params['favorites_by_user'] == 'current_user' || empty($params['favorites_by_user'])) {
            if ($current_user_id = get_current_user_id()) {
                $params['favorites_by_user'] = $current_user_id;
            } else {
                $params['favorites_by_user'] = 0;
            }
        } else if (absint($params['favorites_by_user']) > 0) {
            $params['favorites_by_user'] = absint($params['favorites_by_user']);
        } else {
            $params['favorites_by_user'] = 0;
        }
    }

    // Clean tags
    if (!empty($params['tags'])) {
        if (!is_array($params['tags'])) {
            $comma = _x(',', 'tag delimiter');
            if ( ',' !== $comma ) {
                $params['tags'] = str_replace($comma, ',', $params['tags']);
            }
            $params['tags'] = explode(',', trim($params['tags'], " \n\t\r\0\x0B,"));
            $params['tags'] = array_map('trim', $params['tags']);
        }
    } else {
        $params['tags'] = array();
    }

    /**
     * End of validation
     */
    if (isset($atts['geodir_ajax'])) {
        $params['geodir_ajax'] = $atts['geodir_ajax'];
        unset($atts['geodir_ajax']);
    }
    if (isset($atts['pageno'])) {
        $params['pageno'] = $atts['pageno'];
        unset($atts['pageno']);
    }

    if ( !empty($atts['shortcode_content']) ) {
        $content = $atts['shortcode_content'];
    }
    $params['shortcode_content'] = trim($content);
    $atts['shortcode_content'] = trim($content);
    
    $params['shortcode_atts']       = $atts;

    $output = geodir_sc_gd_listings_output($params);

    return $output;
}