Tạo tìm kiếm theo custom field của Advanced Custom Fields

Thảo luận trong 'Wordpress' bắt đầu bởi xman, 20/10/15.

  1. xman

    xman Administrator Thành viên BQT

    Tham gia ngày:
    6/7/15
    Bài viết:
    43
    Đã được thích:
    2
    Điểm thành tích:
    8
    Tạo widget cho search form:

    PHP:
    class xSearch_Widget extends WP_Widget
    {
        function 
    xSearch_Widget()
        {
            
    parent::WP_Widget(false"xSearch Widget");
        }

        function 
    update($new_instance$old_instance)
        {
            return 
    $new_instance;
        }

        function 
    form($instance)
        {
            
    $title esc_attr($instance["title"]);
            echo 
    "<br />";
        }

        function 
    widget($args$instance)
        {
            
    $widget_id "widget_" $args["widget_id"];
            echo 
    '
    <div class="widget block-sidebar suport-widget" id="ew_online_support-2">
        <div class="block-title" style="margin-bottom: 5px">
            <h3 class="title-module"><i class="fa fa-search"></i> Tìm kiếm</h3>
        </div>
        <div class="search-box" style="padding: 5px;padding-left: 20px">
        <form method="get" action="' 
    home_url() . '/">
        <input type="text" value="' 
    esc_html($s1) . '" name="s" id="s" maxlength="33" style="width:160px" placeholder="Nhập sản phẩm cần tìm"/><br />
        <select name="hangdieuhoa" style="width:160px">
            <option>Hãng điều hòa</option>'
    ;
            
    $args = array(
                
    'orderby'           => 'name',
                
    'order'             => 'ASC',
                
    'hide_empty'        => false,
                
    'parent'            => 0,
                
    'hierarchical'      => true,
            );
            
    $product_categories get_terms'product_cat'$args );
            
    $count count($product_categories);
            if ( 
    $count ){
                foreach ( 
    $product_categories as $product_category ) {
                    echo 
    '<option value="' $product_category-> term_id  '">' $product_category->name '</option>';
                }
            }
    echo 
    '
        </select><br />
        <select name="loaidieuhoa" style="width:160px">
            <option value="0">Loại điều hòa</option>
            <option value="Một chiều">Một chiều</option>
            <option value="Hai chiều">Hai chiều</option>
        </select><br />
        <select name="tietkiemdien" style="width:160px">
            <option value="0">Tiết kiệm điện</option>
            <option value="1">Có</option>
            <option value="2">Không</option>
        </select><br />
        Công suất:<br />
        <select name="congsuattu" style="width:78px">
            <option value="0">Từ 0</option>
            <option value="8000">8.000 BTU</option>
            <option value="10000">10.000 BTU</option>
            <option value="12000">12.000 BTU</option>
            <option value="13000">13.000 BTU</option>
            <option value="14000">14.000 BTU</option>
            <option value="16000">16.000 BTU</option>
            <option value="18000">18.000 BTU</option>
            <option value="24000">24.000 BTU</option>
            <option value="28000">28.000 BTU</option>
            <option value="30000">30.000 BTU</option>
        </select>
        <select name="congsuatden" style="width:78px">
        <option value="0">Đến</option>
        <option value="8000">8.000 BTU</option>
        <option value="10000">10.000 BTU</option>
        <option value="12000">12.000 BTU</option>
        <option value="13000">13.000 BTU</option>
        <option value="14000">14.000 BTU</option>
        <option value="16000">16.000 BTU</option>
        <option value="18000">18.000 BTU</option>
        <option value="24000">24.000 BTU</option>
        <option value="28000">28.000 BTU</option>
        <option value="30000">30.000 BTU</option>
        </select><br />
        <select name="baohanh" style="width:160px">
            <option value="0">Bảo hành</option>
            <option value="1">01 tháng</option>
            <option value="2">02 tháng</option>
            <option value="3">03 tháng</option>
            <option value="4">04 tháng</option>
            <option value="5">05 tháng</option>
            <option value="6">06 tháng</option>
            <option value="7">07 tháng</option>
            <option value="8">08 tháng</option>
            <option value="9">09 tháng</option>
            <option value="10">10 tháng</option>
            <option value="11">11 tháng</option>
            <option value="12">12 tháng</option>
        </select><br />
        <input type="image" src="' 
    get_bloginfo('template_directory') . '/images/search.jpg" value="" style="margin-left: 120px;margin-top: 10px"/>
        </form>
        </div>
    </div>
            '
    ;
        }
    }

    register_widget("xSearch_Widget");
    Xử lý dữ liệu từ search.php trong thư mục theme

    PHP:
                                   <?php

                                    
    // args
                                    
    $args = array(
                                            
    'numberposts' => -1,
                                            
    'post_type' => 'product'
                                    
    );

                                    
    $search esc_attr($_GET['s']);
                                    if (!empty(
    $search)) {
                                        
    $args['s'] = $search;
                                    }

                                    
    $parent_term_id intval($_GET['hangdieuhoa']);
                                    
    $loaidieuhoa esc_attr($_GET['loaidieuhoa']);

                                    if (!empty(
    $parent_term_id)){
                                        
    $args['tax_query'] = array(
                                                array(
                                                        
    'taxonomy' => 'product_cat',
                                                        
    'field' => 'term_id',
                                                        
    'terms' => $parent_term_id,
                                                ),
                                        );
                                    }

                                    if (!empty(
    $loaidieuhoa)) {
                                        if (!empty(
    $parent_term_id)) {
                                            
    $terms get_terms('product_cat', array('parent' => $parent_term_id'hide_empty' => false));
                                            foreach (
    $terms as $value) {
                                                if (
    $loaidieuhoa == $value->name) {
                                                    
    $loaidieuhoaid $value->term_id;
                                                    break;
                                                }
                                            }

                                            if (!empty(
    $loaidieuhoaid)) {
                                                
    $args['tax_query'] = array(
                                                        array(
                                                                
    'taxonomy' => 'product_cat',
                                                                
    'field' => 'term_id',
                                                                
    'terms' => array($parent_term_id$loaidieuhoaid),
                                                        ),
                                                );
                                            } else {
                                                
    $args['tax_query'] = array(
                                                        array(
                                                                
    'taxonomy' => 'product_cat',
                                                                
    'field' => 'term_id',
                                                                
    'terms' => $parent_term_id
                                                        
    ),
                                                );
                                            }
                                        } else {
                                            
    $term get_terms'product_cat',array('name__like' => $loaidieuhoa,'hide_empty'=> false));
                                            foreach (
    $term as $valueterm) {
                                                
    $termid[] = $valueterm->term_id;
                                            }
                                            
    $args['tax_query'] = array(
                                                    array(
                                                            
    'taxonomy' => 'product_cat',
                                                            
    'field' => 'term_id',
                                                            
    'terms' => $termid,
                                                    ),
                                            );
                                        }

                                    }

                                    
    $congsuattu intval($_GET['congsuattu']);
                                    
    $congsuatden intval($_GET['congsuatden']);

                                    if (!empty(
    $congsuattu) && empty($congsuatden)) {

                                        
    $args['meta_query'] = array(
                                                array(
                                                        
    'key' => 'cong_suat',
                                                        
    'value' => $congsuattu,
                                                        
    'type' => 'NUMERIC',
                                                        
    'compare' => '>='
                                                
    )
                                        );
                                    }

                                    if (!empty(
    $congsuatden)) {

                                        
    $args['meta_query'][] = array(
                                                
    'key' => 'cong_suat',
                                                
    'value' => array($congsuattu$congsuatden),
                                                
    'type' => 'NUMERIC',
                                                
    'compare' => 'BETWEEN'
                                        
    );

                                    }

                                    
    $baohanh intval($_GET['baohanh']);

                                    if (!empty(
    $baohanh)) {
                                        
    $args['meta_query'][] = array(
                                                
    'key' => 'bao_hanh',
                                                
    'value' => $baohanh,
                                                
    'type' => 'NUMERIC',
                                                
    'compare' => '='
                                        
    );
                                    }

                                    
    $tietkiemdien intval($_GET['tietkiemdien']);

                                    if (!empty(
    $tietkiemdien)) {
                                        
    $args['meta_query'][] = array(
                                                
    'key' => 'tiet_kiem_dien',
                                                
    'value' => $tietkiemdien,
                                                
    'type' => 'NUMERIC',
                                                
    'compare' => '='
                                        
    );
                                    }

                                    
    $args['tax_query']['relation'] = 'AND';


                                    
    // query
                                    
    $the_query = new WP_Query($args);

                                    while (
    $the_query->have_posts()) : $the_query->the_post();
                                        
    get_template_part('template-parts/content');
                                    endwhile;
                                    
    wp_reset_query();
     

Chia sẻ trang này