Trong bài hướng dẫn này mình sẽ chia sẽ cách làm một form tìm kiếm giống hình bên dưới:
Trong form trên gồm có:
-Nhập từ khóa cần tìm: là tìm trong title của bất động sản
-Mức giá là custom field
-Nhu cầu cũng là custom field
-Loại bds: custom field
-Khu vực lấy danh mục
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function form_search_bds ( $atts ) { $atts = shortcode_atts( array( //'bien1' => 'gia tri mac dinh', //'bien2' => 'gia tri mac dinh', ), $atts ); //bắt đầu cache ob_start(); include "includes/form-search-bds.php"; //tất cả nội dung lưu vào biết $output $output = ob_get_clean(); return $output; } add_shortcode( 'form_search_bds', array($this,'form_search_bds') ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<form action="" method="post" class="wpcf7-form init" id="form-search-bds" > <div> <h3> TÌM KIẾM BẤT ĐỘNG SẢN </h3> <p> <label><input placeholder="Nhập từ khóa cần tìm ..." name="keyword" class="tim-kiem-bds" type="text" value="" title=""></label><br> <span class="wpcf7-form-control-wrap menu-90"> <select name="muc_gia" class="wpcf7-form-control wpcf7-select" aria-invalid="false"> <option value="muc_gia">--Mức giá--</option> <option value="duoi500">Dưới 500tr</option> <option value="500-1ty">Từ 500tr - 1 tỷ</option> </select></span><br> <span class="wpcf7-form-control-wrap menu-257"> <select name="nhu_cau" class="wpcf7-form-control wpcf7-select" aria-invalid="false"> <option value="nhu_cau">--Nhu cầu--</option> <option value="canban">Cần bán</option> <option value="chothue">Cho thuê</option> </select></span><br> <span class="wpcf7-form-control-wrap menu-223"> <select name="loai_bds" class="wpcf7-form-control wpcf7-select" aria-invalid="false"> <option value="loai_bds">--Loại BĐS--</option> <option value="dato">Đất ở</option> <option value="datvuon">Đất vườn</option> <option value="kdc">Khu dân cư</option> </select></span><br> <span class="wpcf7-form-control-wrap menu-991"> <?php $taxonomies = array( 'product_cat' ); $args = array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, ); $khuvucs = get_terms($taxonomies, $args); ?> <select name="khu_vuc" class="wpcf7-form-control wpcf7-select" aria-invalid="false"> <option value="khu_vuc">--Khu vực--</option> <?php if($khuvucs){ foreach($khuvucs as $khuvuc){ ?> <option value="<?php echo $khuvuc->term_id; ?>"><?php echo $khuvuc->name; ?></option> <?php } } ?> </select></span><br> <button type="submit" id="form-search-bds-trigger">Tìm kiếm</button> </p></div> </form> |
Code này anh chị copy bỏ vào luôn file form-search-bds.php dưới cùng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<script> jQuery(function($){ $(document).on('submit', '#form-search-bds', function(e){ e.preventDefault(); var submit_data = { action: 'form_search_bds', data: $('#form-search-bds').serialize() }; console.log(submit_data); $.ajax({ type: 'POST', url: '<?php echo admin_url('admin-ajax.php'); ?>', //url: kitimportelementor_vars.ajax_url, data: submit_data, success: function ( response ) { console.log(response); //var link_url = response.data; window.location.href = response.data; } }); }); }); </script> |
Cơ bản là nó sẽ lấy các filter của việc tiếm kiếm để chuyển đên page hiển thị kết qua
1 2 3 4 5 6 7 8 9 |
add_action("wp_ajax_form_search_bds",array($this,'form_search_bds_ajax') ); add_action("wp_ajax_nopriv_form_search_bds",array($this,'form_search_bds_ajax') ); function form_search_bds_ajax(){ parse_str($_POST['data'],$output); //$output sẽ là dữ liệu submit bằng serialize() //$tham_so = implode("&", $output); $link = get_permalink(1494)."?keyword=".$output['keyword']."&muc_gia=".$output['muc_gia']."&khu_vuc=".$output['khu_vuc']."&loai_bds=".$output['loai_bds']."&nhu_cau=".$output['nhu_cau']; wp_send_json_success($link); } |
Trang này thì get các dữ liệu tìm kiếm trên url và query để trả về kết qua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
<?php /* Template Name: Ket qua tim kiem bds */ get_header(); ?> <div class="shop-page-title category-page-title page-title "> <div class="page-title-inner flex-row medium-flex-wrap container"> <div class="flex-col flex-grow medium-text-center"> <h1 class="shop-page-title is-xlarge">Kết quả tìm kiếm</h1> </div> </div> </div> <main id="main" class=""> <div class="row category-page-row"> <div class="col large-9"> <div class="shop-container"> <div class="woof_products_top_panel_content"> </div> <div class="woof_products_top_panel" style="display: none;"></div><div class="woocommerce-notices-wrapper"></div><div class="products row row-small has-box-vertical large-columns-1 medium-columns-3 small-columns-2"> <?php $muc_gia = $_GET['muc_gia']; $query_muc_gia = array(); if($muc_gia == "duoi500"){ $query_muc_gia = array( 'key' => '_regular_price', 'value' => 500000000, 'compare' => '<', 'type'=> 'NUMERIC' ); }elseif($muc_gia == "500-1ty"){ $query_muc_gia = array( 'key' => '_regular_price', 'value' => array(500000000, 1000000000), 'compare' => 'BETWEEN', 'type'=> 'NUMERIC' ); } $query_loai_bds = array( 'key' => 'loai_bat_dong_san', 'value' => $_GET['loai_bds'], 'compare' => '=', ); $query_nhu_cau = array( 'key' => 'nhu_cau', 'value' => $_GET['nhu_cau'], 'compare' => '=', ); $khu_vuc = (int) $_GET['khu_vuc']; //Tìm kiếm theo 1 tiêu chí if($muc_gia == "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] == "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'] ); }elseif($muc_gia != "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] == "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => array( $query_muc_gia ) ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] != "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] == "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => array( $query_loai_bds ) ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] != "nhu_cau" && $_GET['khu_vuc'] == "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => array( $query_nhu_cau ) ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] != "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => array($khu_vuc), ), ) ); //Tìm kiếm theo 2 tiêu chí }elseif($muc_gia != "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] == "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( $query_muc_gia ) ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] != "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] == "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( $query_loai_bds ) ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] != "nhu_cau" && $_GET['khu_vuc'] == "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( $query_nhu_cau ) ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] != "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => array($khu_vuc), ), ) ); //Tìm kiếm theo 3 tiêu chí }elseif($muc_gia != "muc_gia" && $_GET['loai_bds'] != "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] = "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( 'relation' => 'AND', $query_muc_gia, $query_loai_bds ) ); }elseif($muc_gia != "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] != "nhu_cau" && $_GET['khu_vuc'] = "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( 'relation' => 'AND', $query_muc_gia, $query_nhu_cau ) ); }elseif($muc_gia != "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] != "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( $query_muc_gia ), 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => array($khu_vuc), ), ), ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] != "loai_bds" && $_GET['nhu_cau'] != "nhu_cau" && $_GET['khu_vuc'] == "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( 'relation' => 'AND', $query_loai_bds, $query_nhu_cau ) ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] != "loai_bds" && $_GET['nhu_cau'] == "nhu_cau" && $_GET['khu_vuc'] != "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( $query_loai_bds, ) , 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => array($khu_vuc), ), ), ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] == "loai_bds" && $_GET['nhu_cau'] != "nhu_cau" && $_GET['khu_vuc'] != "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( $query_nhu_cau ) , 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => array($khu_vuc), ), ), ); //Tìm kiếm theo 4 tiêu chí }elseif($muc_gia != "muc_gia" && $_GET['loai_bds'] != "loai_bds" && $_GET['nhu_cau'] != "nhu_cau" && $_GET['khu_vuc'] = "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( 'relation' => 'AND', $query_muc_gia, $query_loai_bds, $query_nhu_cau ) ); }elseif($muc_gia == "muc_gia" && $_GET['loai_bds'] != "loai_bds" && $_GET['nhu_cau'] != "nhu_cau" && $_GET['khu_vuc'] != "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( 'relation' => 'AND', $query_loai_bds, $query_nhu_cau ), 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => array($khu_vuc), ), ), ); //Tìm kiếm theo 5 tiêu chí }elseif($muc_gia != "muc_gia" && $_GET['loai_bds'] != "loai_bds" && $_GET['nhu_cau'] != "nhu_cau" && $_GET['khu_vuc'] != "khu_vuc" ){ $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 's' => $_GET['keyword'], 'meta_query' => array( 'relation' => 'AND', $query_muc_gia, $query_loai_bds, $query_nhu_cau ), 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => array($khu_vuc), ), ), ); } //echo "<pre>"; //print_r($args); //echo "</pre>"; $ketquas = get_posts($args); ?> <?php if($ketquas){ foreach($ketquas as $ketqua){ $product_id = $ketqua->ID; $link = get_permalink($product_id); $thumbnail_id = get_post_thumbnail_id($product_id); $title = $ketqua->post_title; $product = wc_get_product($product_id); ?> <div class="product-small col has-hover product type-product post-1476 status-publish first instock product_cat-dat-nen product_cat-tp-buon-ma-thuot has-post-thumbnail shipping-taxable product-type-simple"> <div class="col-inner"> <div class="badge-container absolute left top z-1"> </div> <div class="product-small box box-vertical"> <div class="box-image"> <div class="image-zoom"> <a href="<?php echo $link; ?>"><?php echo wp_get_attachment_image($thumbnail_id,'woocommerce_thumbnail'); ?></a> </div> </div> <div class="box-text box-text-products text-center grid-style-2"> <div class="title-wrapper"><p class="name product-title woocommerce-loop-product__title"><a href="<?php echo $link; ?>" class="woocommerce-LoopProduct-link woocommerce-loop-product__link"><?php echo $title; ?></a></p></div> <div class="thong-tin-du-an"> <div class="dia-chi"> <i class="fa fa-map-marker"></i><span><?php echo get_post_meta($product_id,"dia_chi",true); ?></span> </div> <div class="phong-ngu"> <i class="fas fa-bed"></i><span><strong>Phòng ngủ: <?php echo get_post_meta($product_id,"phong_ngu",true); ?></strong> </span> </div> <div class="phong-tam"> <i class="fas fa-bath"></i><span><strong>Phòng tắm: <?php echo get_post_meta($product_id,"phong_tam",true); ?></strong></span> </div> <div class="dien-tich"> <i class="fas fa-expand-arrows-alt"></i><span><strong>Diện tích: </strong><?php echo get_post_meta($product_id,"dien_tich",true); ?></span> </div> </div> <div class="bds2"> <div class="price-wrapper"> <span class="price"><?php echo $product->get_price_html(); ?></span> </div> <div class="nut-xem-ngay"><a href="<?php echo $link; ?>" class="btn btn-views bnt_hover_effect">Xem ngay <i class="fas fa-angle-right"></i></a></div> </div> </div> </div> </div> </div> <?php } }else{ ?> <p style="text-align:center">Không tìm thấy kết quả phù hợp</p> <?php } ?> </div><!-- row --> </div><!-- shop container --> </div> <div class="large-3 col hide-for-medium "> <div id="shop-sidebar" class="sidebar-inner"> <?php if ( is_active_sidebar( 'shop-sidebar' ) ) : dynamic_sidebar('shop-sidebar' ); endif; ?> </div> </div> </div> </main> <?php get_footer(); ?> |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress