function trav_acc_get_special_accs($type = 'latest', $count = 10, $exclude_ids = array(), $country = '', $city = '', $acc_type = array()) { $args = array('post_type' => 'accommodation', 'suppress_filters' => 0, 'posts_per_page' => $count); if (!empty($country)) { if (is_numeric($country)) { $args['meta_query'][] = array('key' => 'trav_accommodation_country', 'value' => $country); } else { $term = get_term_by('name', $country, 'location'); if ($term) { $country = $term->term_id; $args['meta_query'][] = array('key' => 'trav_accommodation_country', 'value' => $country); } } } if (!empty($city)) { if (is_numeric($city)) { $args['meta_query'][] = array('key' => 'trav_accommodation_city', 'value' => $city); } else { $term = get_term_by('name', $city, 'location'); if ($term) { $city = $term->term_id; $args['meta_query'][] = array('key' => 'trav_accommodation_city', 'value' => $city); } } } if (!empty($exclude_ids)) { $args['post__not_in'] = $exclude_ids; } if (!empty($acc_type)) { $args['tax_query'] = array(array('taxonomy' => 'accommodation_type', 'field' => 'term_id', 'terms' => $acc_type)); } if ($type == 'featured') { $args = array_merge($args, array('orderby' => 'rand', 'meta_key' => 'trav_accommodation_featured', 'meta_value' => '1')); return get_posts($args); } elseif ($type == 'latest') { $args = array_merge($args, array('orderby' => 'post_date', 'order' => 'DESC')); return get_posts($args); } elseif ($type == 'popular') { global $wpdb; $tbl_postmeta = esc_sql($wpdb->prefix . 'postmeta'); $tbl_terms = esc_sql($wpdb->prefix . 'terms'); $tbl_term_taxonomy = esc_sql($wpdb->prefix . 'term_taxonomy'); $tbl_term_relationships = esc_sql($wpdb->prefix . 'term_relationships'); $date = date('Y-m-d', strtotime('-30 days')); $sql = 'SELECT accommodation_id, COUNT(*) AS booking_count FROM ' . TRAV_ACCOMMODATION_BOOKINGS_TABLE . ' AS booking'; $where = ' WHERE (booking.status <> 0) AND (booking.created > %s)'; if (!empty($country)) { $sql .= " INNER JOIN {$tbl_postmeta} AS meta_c1 ON (meta_c1.meta_key = 'trav_accommodation_country') AND (booking.accommodation_id = meta_c1.post_id)"; $where .= " AND meta_c1.meta_value like '%%{$country}%%'"; } if (!empty($city)) { $sql .= " INNER JOIN {$tbl_postmeta} AS meta_c1 ON (meta_c1.meta_key = 'trav_accommodation_city') AND (booking.accommodation_id = meta_c1.post_id)"; $where .= " AND meta_c1.meta_value like '%%{$city}%%'"; } if (!empty($acc_type)) { $sql .= " INNER JOIN {$tbl_term_relationships} AS tr ON tr.object_id = t1.acc_id \n\t\t\t\t\t\tINNER JOIN {$tbl_term_taxonomy} AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"; $where .= " AND tt.taxonomy = 'accommodation_type' AND tt.term_id IN (" . esc_sql(implode(',', $acc_type)) . ")"; } $sql .= $where . ' GROUP BY booking.accommodation_id ORDER BY booking_count desc LIMIT %d'; $popular_accs = $wpdb->get_results($wpdb->prepare($sql, $date, $count)); $result = array(); if (!empty($popular_accs)) { foreach ($popular_accs as $acc) { $result[] = get_post(trav_acc_clang_id($acc->accommodation_id)); } } // if booked room number in last month is smaller than count then add latest accs if (count($popular_accs) < $count) { foreach ($popular_accs as $acc) { $exclude_ids[] = trav_acc_clang_id($acc->accommodation_id); } $result = array_merge($result, trav_acc_get_special_accs('latest', $count - count($popular_accs), $exclude_ids, $country, $city, $acc_type)); } return $result; } }
function shortcode_accommodations($atts) { extract(shortcode_atts(array('title' => '', 'type' => 'latest', 'style' => 'style1', 'count' => 10, 'count_per_row' => 4, 'city' => '', 'country' => '', 'acc_type' => '', 'post_ids' => '', 'slide' => 'true', 'before_list' => '', 'after_list' => '', 'before_item' => '', 'after_item' => '', 'show_badge' => '', 'animation_type' => '', 'animation_duration' => '', 'animation_delay' => '', 'item_width' => '270', 'item_margin' => '30'), $atts)); if ($slide == 'no' || $slide == 'false') { $slide = 'false'; } if ($type == 'hot' && empty($show_badge)) { $show_badge = true; } if ($show_badge == 'no' || $show_badge == 'false') { $show_badge = false; } $styles = array('style1', 'style2', 'style3', 'style4'); $types = array('latest', 'featured', 'popular', 'hot', 'selected'); if (!in_array($style, $styles)) { $style = 'style1'; } if (!in_array($type, $types)) { $type = 'latest'; } $post_ids = explode(',', $post_ids); $acc_type = !empty($acc_type) ? explode(',', $acc_type) : array(); $count = is_numeric($count) ? $count : 10; $count_per_row = is_numeric($count_per_row) ? $count_per_row : 4; $item_width = is_numeric($item_width) ? $item_width : 270; $item_margin = is_numeric($item_margin) ? $item_margin : 270; $def_before_list = ''; $def_after_list = ''; $def_before_item = ''; $def_after_item = ''; if ($style == 'style4') { $def_before_list = '<div class="listing-style4">'; $def_after_list = '</div>'; } elseif ($style == 'style3') { $def_before_list = '<div class="listing-style3 hotel">'; $def_after_list = '</div>'; } else { if ($slide == 'false') { $def_before_list = '<div class="row hotel image-box listing-' . esc_attr($style) . '">'; $def_after_list = '</div>'; if (2 == $count_per_row) { $def_before_list = '<div class="row hotel image-box listing-' . esc_attr($style) . '">'; $def_before_item = "<div class='col-sm-6'>"; $def_after_item = "</div>"; } elseif (3 == $count_per_row) { $def_before_list = '<div class="row hotel image-box listing-' . esc_attr($style) . '">'; $def_before_item = "<div class='col-sm-4'>"; $def_after_item = "</div>"; } else { $def_before_list = '<div class="row hotel image-box listing-' . esc_attr($style) . '">'; $def_before_item = "<div class='col-sm-6 col-sms-6 col-md-3'>"; $def_after_item = "</div>"; } } else { $def_before_list = '<div class="block image-carousel style2 flexslider" data-animation="slide" data-item-width="' . esc_attr($item_width) . '" data-item-margin="' . esc_attr($item_margin) . '"><ul class="slides hotel image-box listing-' . esc_attr($style) . '">'; $def_after_list = '</ul></div>'; $def_before_item = '<li>'; $def_after_item = '</li>'; } } if (empty($before_list)) { $before_list = $def_before_list; } if (empty($after_list)) { $after_list = $def_after_list; } if (empty($before_item)) { $before_item = $def_before_item; } if (empty($after_item)) { $after_item = $def_after_item; } $accs = array(); if ($type == 'selected') { $accs = trav_acc_get_accs_from_id($post_ids); } elseif ($type == 'hot') { $accs = trav_acc_get_hot_accs($count, $country, $city, $acc_type); } else { $accs = trav_acc_get_special_accs($type, $count, array(), $country, $city, $acc_type); } ob_start(); if (!empty($title)) { echo '<h2>' . esc_html($title) . '</h2>'; } echo $before_list; $i = 0; foreach ($accs as $acc) { $animation = ''; if (!empty($animation_type)) { $animation .= ' class="animated" data-animation-type="' . esc_attr($animation_type) . '" data-animation-duration="' . esc_attr($animation_duration) . '" data-animation-delay="' . esc_attr($animation_delay * $i) . '" '; } trav_acc_get_acc_list_sigle($acc->ID, $style, $before_item, $after_item, $show_badge, $animation); $i++; } echo $after_list; $output = ob_get_contents(); ob_end_clean(); return $output; }