function trav_tour_get_special_tours($type = 'latest', $count = 10, $exclude_ids = array(), $country = '', $city = '', $tour_type = array()) { $args = array('post_type' => 'tour', 'suppress_filters' => 0, 'posts_per_page' => $count); if (!empty($country)) { if (is_numeric($country)) { $args['meta_query'][] = array('key' => 'trav_tour_country', 'value' => $country); } else { $term = get_term_by('name', $country, 'location'); if ($term) { $country = $term->term_id; $args['meta_query'][] = array('key' => 'trav_tour_country', 'value' => $country); } } } if (!empty($city)) { if (is_numeric($city)) { $args['meta_query'][] = array('key' => 'trav_tour_city', 'value' => $city); } else { $term = get_term_by('name', $city, 'location'); if ($term) { $city = $term->term_id; $args['meta_query'][] = array('key' => 'trav_tour_city', 'value' => $city); } } } if (!empty($exclude_ids)) { $args['post__not_in'] = $exclude_ids; } if (!empty($tour_type)) { $args['tax_query'] = array(array('taxonomy' => 'tour_type', 'field' => 'term_id', 'terms' => $tour_type)); } if ($type == 'featured') { $args = array_merge($args, array('orderby' => 'rand', 'meta_key' => 'trav_tour_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 tour_id, COUNT(*) AS booking_count FROM ' . TRAV_TOUR_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_tour_country') AND (booking.tour_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_tour_city') AND (booking.tour_id = meta_c1.post_id)"; $where .= " AND meta_c1.meta_value like '%%{$city}%%'"; } if (!empty($tour_type)) { $sql .= " INNER JOIN {$tbl_term_relationships} AS tr ON tr.object_id = t1.tour_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 = 'tour_type' AND tt.term_id IN (" . esc_sql(implode(',', $tour_type)) . ")"; } $sql .= $where . ' GROUP BY booking.tour_id ORDER BY booking_count desc LIMIT %d'; $popular_tours = $wpdb->get_results($wpdb->prepare($sql, $date, $count)); $result = array(); if (!empty($popular_tours)) { foreach ($popular_tours as $tour) { $result[] = get_post(trav_tour_clang_id($tour->tour_id)); } } // if booked room number in last month is smaller than count then add latest tours if (count($popular_tours) < $count) { foreach ($popular_tours as $tour) { $exclude_ids[] = trav_tour_clang_id($tour->tour_id); } $result = array_merge($result, trav_tour_get_special_tours('latest', $count - count($popular_tours), $exclude_ids, $country, $city, $tour_type)); } return $result; } }
function shortcode_latest_tours($atts) { extract(shortcode_atts(array('count' => 3, 'class' => '', 'thumb_width' => 64, 'thumb_height' => 64), $atts)); $tours = trav_tour_get_special_tours('latest', $count); $result = ''; $result .= '<div class="image-box style14">'; foreach ($tours as $tour) { $tour_id = $tour->ID; $min_price = get_post_meta($tour_id, 'trav_tour_min_price', true); $min_price = empty($min_price) ? 0 : $min_price; $result .= '<article class="box"><figure>'; $result .= '<a href="' . esc_url(get_permalink($tour_id)) . '">' . get_the_post_thumbnail($tour_id, array($thumb_width, $thumb_height)) . '</a>'; $result .= '</figure>'; $result .= '<div class="details">'; $result .= '<h5 class="title"><a href="' . esc_url(get_permalink($tour_id)) . '">' . esc_html(get_the_title($tour_id)) . '</a></h5>'; $result .= '<label class="price-wrapper"><span class="price-per-unit">' . esc_html(trav_get_price_field($min_price)) . '</span> ' . __('per person', 'trav') . '</label>'; $result .= '</div>'; $result .= '</article>'; } $result .= '</div>'; return $result; }