Hướng dẫn search post wordpress - tìm kiếm bài viết wordpress

Để xây dựng tính năng search trong wordpress thì phải nói cực kỳ đơn giản, các bạn chỉ cần vài ba đoạn code nhỏ là có thể làm được. Nhưng… Search theo custom field ? Các bạn đã thử chưa 😀 Nếu chưa thì hôm này mình sẽ hướng dẫn cho các bạn cách để làm chức năng đó.Search theo custom field ? Các bạn đã thử chưa 😀 Nếu chưa thì hôm này mình sẽ hướng dẫn cho các bạn cách để làm chức năng đó.

Để ôn lại 1 tý thì mình sẽ nhắc lại cách tạo 1 form search bình thường trong wordpress. Các bạn xem đoạn code phía dưới nha

Tạo form search trong wordpress

Form search đơn giản nhất

<form action="/"method="GET"role="form"_lpchecked="1">

<input type="text"name="s"id="s-home" autocomplete="off"placeholder="Từ khóa....">

<button type="submit"class="button-search"><iclass="fa fa-search"></i></button>

</form>

Search theo 1 post type nào đó

<form action="/"method="GET"role="form"_lpchecked="1">

<input type="hidden"name="post_type"value="post">

<input type="text"name="s"id="s-home"autocomplete="off"placeholder="Từ khóa....">

<button type="submit"class="button-search"><i class="fa fa-search"></i></button>

</form>

Search theo 1 post type nào đó

Trong ví dụ trên mình search các bài viết thuộc post type là post nha. Các post type khác sẽ không được hiển thị trong kết quả search

<form action="/"method="GET"role="form"_lpchecked="1">

<select name="cat"id="cat"class="form-control">

Search theo 1 post type nào đóoption value="">Chnchuyênmc</option>

$args=array(

Search theo 1 post type nào đó'hide_empty' =>0,

Trong ví dụ trên mình search các bài viết thuộc post type là post nha. Các post type khác sẽ không được hiển thị trong kết quả search'taxonomy'=>'category'

Search theo 1 category nào đó);

Chọnchuyênmục$cates=get_categories($args);

    'hide_empty'=>0,foreach($catesas$cate){  ?>

name;?>option value="echo$cate->term_id; ?>">echo$cate->name;?></option>

}?>

</select>

<input type="text"name="s"id="s-home"autocomplete="off"placeholder="Từ khóa....">

<button type="submit"class="button-search"><i class="fa fa-search"></i></button>

</form>

Search theo 1 post type nào đó

Hướng dẫn search post wordpress - tìm kiếm bài viết wordpress

Trong ví dụ trên mình search các bài viết thuộc post type là post nha. Các post type khác sẽ không được hiển thị trong kết quả search

Search theo 1 category nào đó

Chọnchuyênmục

    'hide_empty'=>0,Tạo form searchxử lý trả dữ liệu

    'taxonomy'=>'category'

    );

<form action="/"method="GET"role="form">

<input type="hidden"name="post_type"value="bat-dong-san">

<input type="text"name="s"id="s-home"autocomplete="off"placeholder="Từ khóa....">

<input type="number"name="min-price"placeholder="Giá thấp nhất">

<input type="number"name="max-price"placeholder="Giá cao nhất">

<button type="submit"class="button-search"><i class="fa fa-search"></i></button>

</form>

Search theo 1 post type nào đóget, khi sumit form website nó sẽ có đường dẫn ở dạng như sau: domain.com/?post_type=bat-dong-san&s=đất+nền&min-price=500&max-price=50000000

Trong ví dụ trên mình search các bài viết thuộc post type là post nha. Các post type khác sẽ không được hiển thị trong kết quả search

Search theo 1 category nào đó

Chọnchuyênmụcsearch.php. Vì thế để xử lý dữ liệu tìm kiếm chúng ta viết trong file search.php đoạn code sau.

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

    'hide_empty'=>0,=isset($_GET['post_type'])&& $_GET['post_type'] ? $_GET['post_type'] : 'post';

    'taxonomy'=>'category'= isset($_GET['min-price'])&& $_GET['min-price'] ? $_GET['min-price'] : 0;

    );=isset($_GET['max-price'])&& $_GET['max-price'] ? $_GET['max-price'] : 99999999999999;

    $cates=get_categories($args);=isset($_GET['s'])&& $_GET['s'] ? $_GET['s'] : '';

$args=array(=array(

'showposts'=>10,=>10,

'post_type'=>$post_type, =>$post_type,

's'=>$key,=>$key,

'meta_query'    =>array(    =>array(

    foreach($catesas$cate){  ?>'relation'      => 'AND',

            array(array(

Đoạn code trên cho phép chọn chuyên mục cần search.'key'       =>'price',

Chức năng search theo custom field trong wordpress'value'     =>$minprice,

                'type'      =>'NUMERIC','type'      =>'NUMERIC',

Để dễ hình dung mình sẽ đưa ra yêu cầù rồi chúng ta sẽ theo yêu cầu mà làm chức năng nhé, yêu cầu như sau:'compare'   => '>='

            ),),

            array(array(

Đoạn code trên cho phép chọn chuyên mục cần search.'key'       => 'price',

Chức năng search theo custom field trong wordpress'value'     =>$maxprice,

                'type'      =>'NUMERIC','type'      => 'NUMERIC',

Để dễ hình dung mình sẽ đưa ra yêu cầù rồi chúng ta sẽ theo yêu cầu mà làm chức năng nhé, yêu cầu như sau:'compare'   =>'<='

            ))

Tạo form search bất động sản theo từ khóa và khoảng giá. Khoảng giá ở đây có 2 giá trị là giá nhỏ nhất và giá lớn nhất, với giá của bất động sản được lưu ở custom field,

Với yêu cầu như trên, chúng ta sẽ thự hiện 2 công việc chính là: Tạo form search và xử lý trả dữ liệu;

Tạo form search theo custom field giá:

$the_query=newWP_Query($args);?>

have_posts()):?>if($the_query->have_posts()):?>

have_posts()):$the_query->the_post();?> while($the_query->have_posts()):$the_query->the_post();?>

<p> the_title();?></p>

endwhile;?>

endif;?>

wp_reset_query();?>

Các bạn chèn đoạn code sau vào vị trí mà các bạn mong muốn hiển thị form search. Chú ý code dưới chỉ là html chưa có giao diện gì cả, các bạn muốn đẹp thì thêm css vào nha.

  • Mình sẽ tạo form search theo phương thức get, khi sumit form website nó sẽ có đường dẫn ở dạng như sau: domain.com/?post_type=bat-dong-san&s=đất+nền&min-price=500&max-price=50000000 thực hiện kiểm tra dữ liệu và lấy dữ liệu được truyền qua thông qua phương thức get $_GET
  • Chúng ta sẽ dựa vào các dữ liệu được truyền trên url để get các giá trị và xử lý. số lượng bất động sản hiển thị là 10
  • Xử lý search dữ liệu và hiển thị kết quả: post_type ở đây sẽ nhận giá trị là ‘bat-dong-san’
  • Chắc các bạn cũng biết kết file hiển thị dữ liệu trả về của form search trong theme wordpress là file search.php. Vì thế để xử lý dữ liệu tìm kiếm chúng ta viết trong file search.php đoạn code sau. điều kiện search theo từ khóa
  • $post_type=isset($_GET['post_type'])&& $_GET['post_type'] ? $_GET['post_type'] : 'post'; đây là khu vực truyền tham số search theo custom field
    • ‘relation’ => ‘AND’ các điều kiện sẽ kết hợp với nhau theo phương thức bằng các điều kiện sẽ kết hợp với nhau theo phương thức bằng
    • Dòng 12 đến dòng 17 điều kiện cho giá trị nhỏ nhất điều kiện cho giá trị nhỏ nhất
    • Dòng 18 đến 23 điều kiện cho giá trị lớn nhất điều kiện cho giá trị lớn nhất
  • Từ dòng 27 đến kết thúc là  vòng lặp get post trong wordpressdòng 27 đến kết thúc là  vòng lặp get post trong wordpress

Một số ví dụ về search theo custom field khác

Ví dụ search các bài viết có trên 100 lượt views

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

$args=array(=array(

    'numberposts'   =>10,'numberposts'   =>10,

    'post_type'     =>'post','post_type'     =>'post',

    'meta_query'    =>array('meta_query'    => array(

        array(array(

            'key'       =>'views','key'       =>'views',

            'value'     =>100,'value'     =>100,

            'type'      =>'NUMERIC','type'      =>'NUMERIC',

            'compare'   =>'>''compare'   =>'>'

        ))

    ))

);;

$the_query=newWP_Query($args);?>=newWP_Query($args);?>

have_posts()):?>if($the_query->have_posts() ):?>

    <ul>

    have_posts()):$the_query->the_post();?>while($the_query->have_posts()):$the_query->the_post(); ?>

        <li>

            <ahref="the_permalink();?>">

                <img src="the_field('event_thumbnail');?>"/>

                the_title();?>

            </a>

        </li>

    endwhile;?>

    </ul>

endif;?>

wp_reset_query();?>

    'numberposts'   =>10,

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

$args=array(=array(

    'numberposts'   =>10,'numberposts'   =>10,

    'post_type'     =>'post','post_type'     =>'product',

    'meta_query'    =>array('meta_query'    => array(

        'relation'      =>'AND','relation'      =>'AND',

        array(array(

            'key'       =>'views','key'       =>'total_sales',

            'value'     =>100,'value'     =>10,

            'type'      =>'NUMERIC','type'      => 'NUMERIC',

            'compare'   =>'>''compare'   =>'>'

        ),),

        array(array(

);'key'       =>'color_product',

Search 10 sản phẩm có trên 10 lượt mua và màu sắc là màu đỏ.'value'     =>'red',

    'post_type'     =>'product','compare'   =>'='

        ))

    ))

);;

$the_query=newWP_Query($args);?>=newWP_Query($args);?>

have_posts()):?>if($the_query->have_posts() ):?>

    <ul>

    have_posts()):$the_query->the_post();?>while($the_query->have_posts()):$the_query->the_post(); ?>

        <li>

            <ahref="the_permalink();?>">

                <img src="the_field('event_thumbnail');?>"/>

                the_title();?>

            </a>

        </li>

    endwhile;?>

    </ul>

endif;?>

wp_reset_query();?>

    'numberposts'   =>10,

    'post_type'     =>'post',search theo custom field cũng như là query các dữ liệu có liên quan đến custom field. Biết được món này thì bạn không phải lo xử lý các vấn đề trong việc làm các site bất động sản hoặc site du lịch.

            'key'       =>'views',

            'value'     =>100,