Bộ lọc tìm kiếm tùy chỉnh trong WordPress

Trình tạo trang tìm kiếm WordPress tùy chỉnh nâng cao miễn phí sẽ cho phép bạn tạo các bộ lọc tìm kiếm nâng cao bằng cách sử dụng các loại bài đăng tùy chỉnh, phân loại tùy chỉnh và các trường khác. Nó cho phép bạn chọn các trường biểu mẫu ưa thích để lọc và sẽ tạo mã cần thiết cho trang tìm kiếm của bạn. Nó cũng bao gồm một biểu mẫu tìm kiếm trong đó bạn có thể chọn có các tùy chọn menu thả xuống, nhập văn bản cho từ khóa và hộp kiểm

Show

Nó sẽ tạo mã đầy đủ mà bạn có thể sử dụng làm mẫu cho trang tùy chỉnh, chẳng hạn như tìm kiếm công thức. php. Nó bao gồm một biểu mẫu tìm kiếm được nhúng, một vòng kết quả và phân trang

Các đối số tiêu chuẩn được tự động thêm vào;

Nếu bạn sử dụng tham số “lấy” trong biểu mẫu của mình (so với “đăng”), url của bạn với các chuỗi truy vấn có thể trông như thế này.
https. //tên miền của tôi. com/recipe-search/?keyword=orange&cuisine=chinese&course=dinner

Tạo hộp tìm kiếm trường văn bản trong biểu mẫu của bạn

Phân loại chọn DropDown?

Tạo một menu thả xuống danh sách chọn bằng cách sử dụng tên phân loại

Tạo danh sách hộp kiểm bằng tên phân loại

Tạo mảng post_type (phải thêm 'dấu nháy đơn' và phân tách bằng dấu phẩy)

Thu hẹp kết quả dựa trên giá trị của trường tùy chỉnh. Dựa trên https. // nhà phát triển. báo chí. org/reference/classes/wp_query/#custom-field-post-meta-parameters

Carlo là một nhà thiết kế và phát triển front-end tự do. Trong những năm qua, anh ấy đã viết cho ioProgrammo, một tạp chí máy tính in của Ý và … Thông tin thêm về Carlo ↬

Bản tin email

Email (đập vỡ) của bạn

Mẹo hàng tuần về giao diện người dùng & UX.
Được hơn 200.000 người tin cậy.

  • Bộ lọc tìm kiếm tùy chỉnh trong WordPress
    Danh sách kiểm tra thiết kế giao diện thông minh

  • Bộ lọc tìm kiếm tùy chỉnh trong WordPress
    Các mẫu thiết kế giao diện Đào tạo UX

  • Bộ lọc tìm kiếm tùy chỉnh trong WordPress
    Đập tanConf Freiburg. Tất cả về Web. Ngày 4–6 tháng 9 năm 2023

  • Bộ lọc tìm kiếm tùy chỉnh trong WordPress
    Bắt đầu miễn phí
  • Bộ lọc tìm kiếm tùy chỉnh trong WordPress
    Giao diện người dùng SmashingConf 2023

Bài viết này sẽ chỉ cho bạn cách cung cấp cho cài đặt WordPress của bạn một hệ thống tìm kiếm nâng cao cho phép người dùng tìm kiếm và truy xuất nội dung từ một loại bài đăng tùy chỉnh cụ thể, lọc kết quả theo thuật ngữ phân loại tùy chỉnh và nhiều giá trị trường tùy chỉnh. Nó sẽ bao gồm cả phần giới thiệu lý thuyết để xử lý các yêu cầu của người dùng và ứng dụng cụ thể của lý thuyết đó, đặc biệt là xây dựng một hệ thống tìm kiếm nâng cao

Nhiều siêu năng lực của WordPress đến từ kiến ​​trúc dữ liệu linh hoạt của nó, cho phép các nhà phát triển tùy chỉnh rộng rãi các cài đặt của họ với các loại bài đăng, phân loại và trường tùy chỉnh. Tuy nhiên, khi nói đến tìm kiếm của nó, WordPress chỉ cung cấp cho chúng tôi biểu mẫu một trường thường không phù hợp và khiến quản trị viên trang web áp dụng các hệ thống tìm kiếm bên ngoài, như Tìm kiếm tùy chỉnh của Google hoặc plugin của bên thứ ba

Trong bài viết này, tôi sẽ chỉ cho bạn cách cung cấp cho bản cài đặt WordPress của bạn một hệ thống tìm kiếm nâng cao cho phép người dùng tìm kiếm và truy xuất nội dung từ một loại bài đăng tùy chỉnh cụ thể, lọc kết quả theo thuật ngữ phân loại tùy chỉnh và nhiều giá trị trường tùy chỉnh

Bài viết có hai phần. Đầu tiên, tôi sẽ trình bày phần giới thiệu lý thuyết về xử lý yêu cầu của người dùng, bắt đầu từ việc truyền URL, chuyển qua thực thi truy vấn và kết thúc bằng sản xuất đầu ra. Phần thứ hai của bài viết là một ứng dụng cụ thể của những gì chúng ta sẽ học trong phần đầu tiên và ở đó chúng ta sẽ xây dựng hệ thống tìm kiếm nâng cao của mình

Vì vậy, hãy bắt đầu tìm hiểu một số khái niệm chính

Yêu cầu người dùng

Khi người dùng nhấp vào một liên kết hoặc nhập URL trỏ đến một trang của trang web, WordPress sẽ thực hiện một loạt các thao tác được mô tả rõ trong Tổng quan về truy vấn tham chiếu mã truy vấn WP. Tóm lại, đây là những gì xảy ra

  1. WordPress phân tích cú pháp URL được yêu cầu thành một tập hợp các tham số truy vấn (được gọi là đặc tả truy vấn)
  2. Tất cả các biến
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    5 liên quan đến truy vấn được đặt
  3. Đặc tả truy vấn được chuyển đổi thành truy vấn MySQL, được thực thi đối với cơ sở dữ liệu
  4. Tập dữ liệu đã truy xuất được lưu trữ trong đối tượng
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    6
  5. WordPress sau đó xử lý lỗi 404
  6. WordPress gửi các tiêu đề HTTP blog
  7. Các biến Loop được khởi tạo
  8. Tệp mẫu được chọn theo quy tắc phân cấp mẫu
  9. WordPress chạy Vòng lặp

Phân tích cú pháp URL được ưu tiên trước, vì vậy, hãy đi sâu vào các chuỗi và biến truy vấn

Biến truy vấn WP. Mặc định và biến tùy chỉnh

Trạng thái tham chiếu mã truy vấn WP tại WordPress Query Vars

“Một loạt các lọ truy vấn có sẵn cho người dùng hoặc nhà phát triển WordPress sử dụng để truy vấn các loại nội dung cụ thể hoặc để hỗ trợ chức năng và thiết kế chủ đề và/hoặc plugin. ”

— Tham chiếu mã truy vấn WP

Nói cách khác, các biến truy vấn WordPress là các biến đó trong chuỗi truy vấn xác định (hoặc ảnh hưởng) đến kết quả trong một truy vấn được thực hiện đối với cơ sở dữ liệu. Theo mặc định, WordPress cung cấp các lọ truy vấn công khai và riêng tư và Tham chiếu mã truy vấn WP xác định chúng như sau

“Các lọ truy vấn công khai là những lọ có sẵn và có thể sử dụng thông qua truy vấn URL trực tiếp ở dạng
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
7. Không thể sử dụng các lọ truy vấn riêng tư trong URL, mặc dù WordPress sẽ chấp nhận chuỗi truy vấn có các lọ truy vấn riêng tư, các giá trị sẽ không được chuyển vào truy vấn và phải được đặt trực tiếp vào truy vấn. Một ví dụ được đưa ra dưới đây.

______08”

— Tham chiếu mã truy vấn WP

Do đó, không thể gửi qua chuỗi truy vấn các lọ riêng tư như

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
9,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
0,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
1, v.v. Kiểm tra Tham chiếu mã truy vấn WP để biết danh sách toàn diện về

Với các biến công khai theo ý của chúng tôi (với tư cách là người dùng cũng như nhà phát triển), chúng tôi có thể tập hợp một số lượng lớn truy vấn mà không cần phát triển plugin hoặc chỉnh sửa tệp chức năng của chủ đề. Chúng ta chỉ cần tạo một URL, thêm vào chuỗi truy vấn một hoặc nhiều tham số có sẵn và WordPress sẽ hiển thị kết quả được yêu cầu cho người dùng

Ví dụ: chúng ta có thể truy vấn một loại bài đăng cụ thể bằng cách thêm tham số

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
2 vào chuỗi truy vấn; . Chẳng hạn, chúng ta có thể tạo URL sau

mywebsite.com/?post_type=movie&genre=thriller

WordPress sẽ truy vấn cơ sở dữ liệu và truy xuất tất cả các loại bài đăng

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
4 thuộc thể loại
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
5, trong đó
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
6 là phân loại tùy chỉnh

Thật tuyệt vời, nhưng đó không phải là tất cả. Trên thực tế, những gì chúng ta đã nói cho đến nay chỉ liên quan đến các chức năng tích hợp sẵn của truy vấn vars. WordPress cho phép chúng tôi tiến xa hơn và tạo các biến truy vấn tùy chỉnh của riêng mình

Đăng ký Vars truy vấn tùy chỉnh

Trước khi chúng tôi có thể sử dụng chúng, các lọ truy vấn tùy chỉnh phải được đăng ký. Chúng tôi có thể hoàn thành nhiệm vụ này nhờ bộ lọc

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
7. Vì vậy, hãy mở tệp chính của plugin (hoặc chức năng của chủ đề. php) và viết đoạn mã sau

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
4

Hàm gọi lại giữ một mảng các biến làm đối số và phải trả về cùng một mảng khi các biến mới được thêm vào

Bây giờ chúng ta có thể bao gồm các biến mới trong các tham số sẽ ảnh hưởng đến truy vấn. Các tham số này sẽ có sẵn trong tập lệnh của chúng tôi nhờ thẻ mẫu

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
8, như chúng ta sẽ thấy sau. Bây giờ là lúc để giới thiệu lớp quản lý các truy vấn trong WordPress

Bệ Hạ // An array of arguments $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' ); // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) : $the_query->the_post(); // Your code here endwhile; } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); 9

Truy vấn cơ sở dữ liệu không phải là một nhiệm vụ dễ dàng. Đó không chỉ là vấn đề xây dựng truy vấn hiệu quả mà còn là vấn đề đòi hỏi các vấn đề bảo mật phải được tính đến cẩn thận. Nhờ Lớp

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9, WordPress cho phép chúng tôi truy cập vào cơ sở dữ liệu một cách nhanh chóng (không cần phải nhúng tay vào SQL) và bảo mật (
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9 xây dựng các truy vấn an toàn phía sau hậu trường)

Tập dữ liệu được truy xuất sẽ có sẵn để sử dụng trong Vòng lặp, nhờ có nhiều phương thức và thuộc tính của lớp

Hãy lấy một Vòng lặp chung làm ví dụ

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>

Nếu bạn chưa quen với việc phát triển WordPress, bạn có thể hỏi. “Này, anh bạn. Truy vấn ở đâu?

Trên thực tế, bạn không cần tạo một phiên bản mới của đối tượng

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9. Lớp tự thiết lập truy vấn được thực hiện theo trang yêu cầu. Vì vậy, nếu người xem trang web yêu cầu lưu trữ danh mục, WordPress sẽ chạy truy vấn truy xuất tất cả các bài đăng thuộc danh mục cụ thể đó và Vòng lặp sẽ hiển thị chúng

Nhưng đây chỉ là một ví dụ cơ bản khác nhau của một truy vấn chính. Chúng ta có thể làm được nhiều hơn thế và lọc tập kết quả trả về một cách chi tiết, chỉ bằng cách chuyển một mảng tham số cho một thể hiện mới của lớp

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9, như chúng ta sẽ làm trong ví dụ sau

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Mọi thứ có vẻ phức tạp hơn một chút, phải không?

Phiên bản mới của

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9 giữ một mảng các đối số sẽ ảnh hưởng đến dữ liệu được truy xuất từ ​​cơ sở dữ liệu. Tham chiếu mã truy vấn WP cung cấp, nhóm chúng thành mười bảy danh mục. Vì vậy, chẳng hạn, chúng ta có , , chỉ một (
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
45), , v.v. (chúng ta sẽ quay lại thông số
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9 sau giây lát)

Bây giờ chúng ta đã khởi tạo đối tượng

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47, chúng ta có thể truy cập tất cả các đối tượng của nó.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
48 kiểm tra xem có bài đăng nào còn được in hay không, trong khi
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
49 di chuyển Vòng lặp tới bài viết tiếp theo và cập nhật biến toàn cục
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
90

Bên ngoài Vòng lặp, khi sử dụng truy vấn tùy chỉnh, chúng ta phải luôn thực hiện cuộc gọi tới

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
91. Hàm này khôi phục biến toàn cục
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
90 sau khi thực hiện truy vấn tùy chỉnh và cần thiết vì mọi truy vấn mới sẽ ghi đè lên
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
90. Từ Tham chiếu mã truy vấn WP

“Ghi chú. Nếu bạn sử dụng `the_post()` với truy vấn của mình, bạn cần chạy `wp_reset_postdata()` sau đó để Thẻ mẫu sử dụng lại bài đăng hiện tại của truy vấn chính. ”

— Tham chiếu mã truy vấn WP

Bây giờ hãy quay lại truy vấn args

Thêm sau khi nhảy. Tiếp tục đọc bên dưới ↓

Gặp Bản tin Email Smashing với các mẹo hữu ích về giao diện người dùng, thiết kế & UX. Đăng ký và nhận “Danh sách kiểm tra thiết kế giao diện thông minh” — một bộ tài liệu PDF miễn phí với hơn 150 câu hỏi để bạn tự hỏi mình khi thiết kế và xây dựng hầu hết mọi thứ

Email (đập vỡ) của bạn

Mỗi tuần một lần. Mẹo hữu ích về front-end & UX. Được tin cậy bởi 190. 000 người thân thiện

// An array of arguments $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' ); // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) : $the_query->the_post(); // Your code here endwhile; } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); 9 Lập luận

Chúng tôi đã nói rằng lớp

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9 giữ một mảng cho phép các nhà phát triển chọn chi tiết các kết quả từ cơ sở dữ liệu

Nhóm đầu tiên, Tham số tác giả, bao gồm các đối số cho phép chúng tôi tạo truy vấn dựa trên (các) tác giả của bài đăng (trang và loại bài đăng). Chúng bao gồm

  • <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    96
  • <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    97
  • <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    98
  • <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    99

Nếu bạn muốn lấy tất cả các bài đăng từ

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
90, bạn chỉ cần đặt truy vấn sau

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
4

Nhóm thứ hai bao gồm các Tham số Danh mục, i. e. tất cả những đối số đó cho phép chúng tôi truy vấn (hoặc loại trừ) các bài đăng được chỉ định cho một hoặc nhiều danh mục

  • <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    91
  • <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    92
  • <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    93
  • <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    9
  • // An array of arguments
    $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
            // Your code here
        endwhile;
    
    } else {
            // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    0

Nếu chúng tôi cần tất cả các bài đăng được gán cho danh mục

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
96, chúng tôi chỉ cần đặt đối số
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
92, như chúng tôi sẽ làm trong ví dụ sau

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
9

Truy vấn sau đây tìm kiếm các bài đăng từ nhiều danh mục, dấu phẩy đại diện cho `OR

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
9

Chúng tôi cũng có thể yêu cầu các bài đăng thuộc cả hai danh mục

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
96 và
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
99, cộng với (
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
20AND

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
2

Và chúng ta cũng có thể chuyển một mảng ID, như trong các ví dụ tiếp theo

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
4

Và như vậy với các thẻ và nguyên tắc phân loại, từ khóa tìm kiếm, bài đăng, trang và loại bài đăng. Tham khảo Tham chiếu mã truy vấn WP để biết đối số truy vấn

Như chúng tôi đã nói trước đây, chúng tôi cũng có thể đặt nhiều đối số và truy xuất, ví dụ, tất cả các bài đăng trong một danh mục cụ thể

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
21 được viết bởi một tác giả nhất định

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
6

Khi kiến ​​trúc dữ liệu trở nên phức tạp hơn — và điều đó xảy ra khi chúng tôi thêm các trường tùy chỉnh và nguyên tắc phân loại vào các loại bài đăng — thì có thể cần phải đặt một hoặc nhiều loại cho phép chúng tôi truy xuất tất cả các bài đăng (hoặc loại bài đăng tùy chỉnh) được gắn nhãn với các giá trị trường tùy chỉnh cụ thể. Trong thời gian ngắn, chúng ta sẽ cần thực hiện một truy vấn meta đối với cơ sở dữ liệu

Truy vấn meta WordPress

Tham chiếu mã truy vấn WP cho chúng tôi biết rằng khi xử lý một truy vấn meta,

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9 sử dụng lớp
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
23. Lớp này, được giới thiệu trong WordPress 3. 2, xây dựng mã SQL của các truy vấn dựa trên các trường tùy chỉnh

Để tạo truy vấn dựa trên một trường tùy chỉnh, chúng tôi chỉ cần một hoặc nhiều đối số sau

  • // An array of arguments
    $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
            // Your code here
        endwhile;
    
    } else {
            // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    24
  • // An array of arguments
    $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
            // Your code here
        endwhile;
    
    } else {
            // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    25
  • // An array of arguments
    $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
            // Your code here
        endwhile;
    
    } else {
            // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    26
  • // An array of arguments
    $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
            // Your code here
        endwhile;
    
    } else {
            // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    27

Giả sử một loại bài đăng tùy chỉnh có tên là

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
28. Hãy gán cho mỗi
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
28 một trường tùy chỉnh có tên là
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
40, lưu trữ tên của một vị trí địa lý. Với truy vấn meta, chúng tôi có thể truy xuất từ ​​cơ sở dữ liệu tất cả các chỗ ở trong thành phố đã chỉ định, chỉ cần chuyển các đối số phù hợp cho truy vấn, như bạn có thể thấy bên dưới

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
40

Khi chúng tôi đã đặt đối số, chúng tôi có thể tạo truy vấn theo cách tương tự như trước đây

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
41

Sao chép và dán mã ở trên vào một tệp mẫu và bạn sẽ nhận được một kho lưu trữ tất cả các chỗ ở có sẵn ở Freiburg

Đây là trường hợp của một trường tùy chỉnh duy nhất. Nhưng nếu chúng ta cần tạo truy vấn dựa trên nhiều trường tùy chỉnh thì sao?

Đối số // An array of arguments $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' ); // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) : $the_query->the_post(); // Your code here endwhile; } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); 41

Đối với loại truy vấn này, lớp

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
23 (và cả lớp
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9) cung cấp tham số
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
41. Đây phải là một mảng của các mảng, như trong ví dụ sau

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
42

Phần tử

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
41 là một mảng hai chiều có các mục là truy vấn meta đơn với các đối số sau

ArgumentTypeDescription____546stringXác định trường tùy chỉnh.
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
47chuỗi. mảngCó thể là một mảng chỉ khi giá trị
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
48 là
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
49,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
60,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
61 hoặc
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
62.
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
48stringMột toán tử so sánh. Các giá trị được chấp nhận là
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
64,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
65,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
66,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
67,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
68,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
69,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
400,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
401,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
49,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
60,
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
61,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
405,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
406,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
407,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
408,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
409. Nó mặc định là
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
64.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
411stringLoại trường tùy chỉnh. Các giá trị có thể là
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
412,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
413,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
414,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
415,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
416,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
417,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
418,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
419,
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
420. Nó mặc định là
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
414

Nếu chúng tôi đặt nhiều trường tùy chỉnh, chúng tôi cũng phải gán đối số

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
422 cho phần tử
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
41

Bây giờ chúng ta có thể xây dựng một truy vấn nâng cao hơn. Hãy bắt đầu bằng cách thiết lập các đối số và tạo một phiên bản mới của

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
43

Ở đây, đối số

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
41 chứa hai mảng truy vấn meta và tham số thứ ba thiết lập mối quan hệ giữa các truy vấn meta. Truy vấn tìm kiếm bảng wp_posts cho tất cả các loại bài đăng
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
28 trong đó các trường tùy chỉnh
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
40 và
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
411 lưu trữ tương ứng các giá trị
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
429 và
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
430

Hãy sao chép và dán mã vào một tệp mẫu có tên là lưu trữ-chỗ ở. php. Khi được yêu cầu, WordPress sẽ thực hiện truy vấn tìm kiếm bảng wp_posts và Vòng lặp sẽ hiển thị kết quả, nếu có

Tại thời điểm này, chúng tôi vẫn đang viết mã trong tệp mẫu. Điều này có nghĩa là tập lệnh của chúng tôi là tĩnh và mỗi khi Vòng lặp chạy, nó sẽ tạo ra cùng một đầu ra. Nhưng chúng tôi cần cho phép người dùng trang web thực hiện các yêu cầu tùy chỉnh và để hoàn thành nhiệm vụ này, chúng tôi cần tạo động các truy vấn tùy chỉnh

Bộ lọc <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <!-- code here --> <?php endwhile; else : ?> <!-- code here --> <?php endif; ?> 431

Móc hành động

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
431 được kích hoạt sau khi tạo đối tượng
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47, nhưng trước khi thực thi. Để sửa đổi truy vấn, chúng tôi sẽ phải nối một cuộc gọi lại tùy chỉnh tới
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
431

Trong một ví dụ trước đó, chúng tôi đã truy vấn cơ sở dữ liệu để truy xuất tất cả các bài đăng trong danh mục

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
96 từ một tác giả nhất định. Trong ví dụ sau, chúng tôi sẽ chuyển các đối số tương tự cho đối tượng
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47, nhưng chúng tôi sẽ không thực hiện công việc với tệp mẫu, như chúng tôi đã làm trước đây, mà chúng tôi sẽ sử dụng tệp chính của plugin (hoặc tệp . php), thay vào đó. Hãy viết khối mã sau

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
44

Đối tượng

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47 được chuyển đến hàm gọi lại theo tham chiếu, không phải theo giá trị và điều này có nghĩa là mọi thay đổi được thực hiện đối với truy vấn đều ảnh hưởng trực tiếp đến đối tượng
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47 ban đầu. Tham chiếu mã truy vấn WP nói

“Hành động
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
431 cung cấp cho nhà phát triển quyền truy cập vào đối tượng
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47 theo tham chiếu (bất kỳ thay đổi nào bạn thực hiện đối với
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47 đều được thực hiện trực tiếp với đối tượng ban đầu — không cần giá trị trả về). ”

— Tham chiếu mã truy vấn WP

Khi chúng ta đang thao tác với đối tượng

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47 ban đầu, chúng ta phải chú ý đến truy vấn mà chúng ta đang thực hiện. Phương thức
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
443 kiểm tra xem đối tượng
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47 hiện tại có phải là… (có. ) truy vấn chính. Tham chiếu mã truy vấn WP cũng cho chúng tôi biết rằng bộ lọc
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
431 có thể ảnh hưởng đến bảng quản trị cũng như các trang giao diện người dùng. Vì lý do này, bạn cũng nên kiểm tra trang được yêu cầu bằng thẻ điều kiện
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
446

Móc hành động

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
431 được ghi lại đầy đủ và rất đáng để đọc Tham khảo mã truy vấn WP để biết mô tả chi tiết hơn và một số ví dụ về cách sử dụng

Bây giờ chúng ta có thể kết thúc phần giới thiệu về các công cụ chính có sẵn để xử lý các truy vấn WordPress. Bây giờ là lúc trình bày một ví dụ cụ thể về việc sử dụng chúng, xây dựng một hệ thống tìm kiếm nâng cao về nội dung trang web. Nghiên cứu trường hợp của chúng tôi được cung cấp bởi một trang web bất động sản

Từ lý thuyết đến mã. Xây dựng hệ thống tìm kiếm

Chúng tôi sẽ làm theo các bước sau

  1. Xác định cấu trúc dữ liệu
  2. Đăng ký vars truy vấn tùy chỉnh
  3. Nhận các giá trị var truy vấn và sử dụng chúng để tạo truy vấn tùy chỉnh
  4. Xây dựng biểu mẫu tạo các giá trị trường theo chương trình

1. Xác định cấu trúc dữ liệu

Mục đích của các loại bài đăng tùy chỉnh là thêm nội dung hợp lý không thể được đưa vào các bài đăng trên blog cũng như trong các trang tĩnh. Các loại bài đăng tùy chỉnh đặc biệt thích hợp để trình bày các sự kiện, sản phẩm, sách, phim, mục danh mục, v.v. Ở đây chúng tôi sẽ xây dựng một kho lưu trữ các quảng cáo bất động sản với cấu trúc sau

  • loại bài tùy chỉnh.
    // An array of arguments
    $args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
            // Your code here
        endwhile;
    
    } else {
            // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    28
  • phân loại tùy chỉnh.
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    449 (B&B, homestay, khách sạn, v.v. )
  • trường tùy chỉnh.
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    450 (cả nhà, phòng riêng, phòng chung)
  • trường tùy chỉnh.
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <!-- code here -->
    <?php endwhile; else : ?>
        <!-- code here -->
    <?php endif; ?>
    
    451
  • các trường tùy chỉnh khác

Chúng ta phải đăng ký loại bài đăng, phân loại tùy chỉnh và các trường tùy chỉnh và hộp meta, như thể hiện trong hình bên dưới

Bộ lọc tìm kiếm tùy chỉnh trong WordPress
Trang chỉnh sửa chỗ ở hiển thị tất cả các trường và hộp meta tùy chỉnh. (Xem bản lớn)

Hình ảnh cho thấy cách trang Chỉnh sửa chỗ ở sẽ xuất hiện sau khi ba hộp meta tùy chỉnh chứa phân loại tùy chỉnh Typology và một số trường tùy chỉnh đã được đăng ký

Mục tiêu của chúng tôi không phải là phân tích kiến ​​trúc dữ liệu WordPress, vì chủ đề này đã được Daniel, Brian, Kevin, Josh và Justin đề cập ở đây trên Tạp chí Smashing. Đọc bài viết của họ nếu bạn cần ôn lại và quay lại ngay khi bạn sẵn sàng

Khi chúng tôi đã xác định kiến ​​trúc dữ liệu, đã đến lúc đăng ký vars truy vấn

2. Đăng ký các biến truy vấn

Trước đó, chúng tôi đã định nghĩa các vars truy vấn là các cặp

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
452 sau dấu chấm hỏi trong URL. Nhưng trước khi chúng tôi có thể xử lý các cặp này trong tập lệnh của mình, chúng tôi phải đăng ký chúng trong tệp plugin hoặc chức năng. Đối với mục đích của chúng tôi, chúng tôi chỉ cần hai biến sẽ cho phép thực hiện truy vấn dựa trên các giá trị của các trường tùy chỉnh tương ứng

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
45

Đó là nó. Chúng tôi đã thêm hai tham số để truy vấn cơ sở dữ liệu. Bây giờ, thật hợp lý khi xây dựng một URL như thế này

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
46

3. Thao tác truy vấn

Bây giờ, hãy thêm một khối mã mới vào tập lệnh của chúng ta

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47

Mã này là tổng của tất cả những gì chúng tôi đã đề cập trong phần đầu tiên của bài viết. Hàm gọi lại sẽ thoát nếu người dùng ở trong bảng quản trị, nếu truy vấn hiện tại không phải là truy vấn chính và nếu loại bài đăng không phải là

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
28

Sau đó, hàm sẽ kiểm tra xem có bất kỳ lọ truy vấn nào mà chúng tôi đã đăng ký trước đó không. Nhiệm vụ này được hoàn thành nhờ hàm

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
454, hàm này truy xuất một biến truy vấn công khai từ một yêu cầu HTTP. Đọc thêm về
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
454 trong Tham khảo mã truy vấn WP. Nếu biến tồn tại, thì hàm gọi lại xác định một mảng cho mỗi truy vấn meta và đẩy nó vào mảng
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
456 đa chiều

Cuối cùng, nếu có ít nhất hai truy vấn meta, thì đối số

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
422 được đẩy vào
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
456 và giá trị của nó được đặt thành
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
459. Sau khi thực hiện xong, phương thức
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
460 sẽ lưu
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
47 để thực hiện tiếp theo

Bạn không cần phải lo lắng về việc làm sạch dữ liệu ở đây, bởi vì các lớp

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9 và
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
23 thực hiện công việc cho chúng tôi — kiểm tra mã nguồn
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9 và
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
23

4. Xây dựng biểu mẫu tìm kiếm

Dữ liệu biểu mẫu được gửi bằng phương thức

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
466. Điều này có nghĩa là các thuộc tính
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
467 và
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
47 của các trường biểu mẫu được gửi dưới dạng các biến URL (i. e. dưới dạng vars truy vấn). Vì vậy, chúng tôi sẽ cung cấp cho các thuộc tính
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
467 của các trường biểu mẫu các giá trị giống như các vars truy vấn đã đăng ký trước đó (
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
40 và
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
411), trong khi các giá trị trường có thể được chỉ định theo chương trình, truy xuất dữ liệu từ cơ sở dữ liệu hoặc do người dùng điền vào

Trước tiên, chúng tôi sẽ tạo một mã ngắn cho phép quản trị viên trang đưa biểu mẫu tìm kiếm vào các bài đăng và trang của trang web. Mã ngắn của chúng tôi sẽ được nối với hành động

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
472

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
48

Tiếp theo, chúng ta sẽ xác định hàm gọi lại sẽ tạo HTML của biểu mẫu chứa ba trường chọn, tương ứng với phân loại tùy chỉnh và hai trường tùy chỉnh

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
49

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
473 là một mảng các thuộc tính shortcode. Bên trong chức năng, chúng tôi sẽ thêm đoạn mã sau

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
0

Chúng tôi đã tạo một truy vấn sẽ truy xuất tất cả các loại bài đăng

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
28 bằng cách đặt trường tùy chỉnh có tên là
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
475 (dấu gạch dưới trước tên đại diện cho trường tùy chỉnh ẩn)

Vòng lặp sẽ không hiển thị bất kỳ chỗ ở nào, nhưng sẽ thêm các phần tử vào mảng

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
476 từ giá trị trường tùy chỉnh tương ứng. Điều kiện sẽ bỏ qua trùng lặp. Nếu không có chỗ ở, việc thực hiện bị gián đoạn;

Bộ lọc tìm kiếm tùy chỉnh trong WordPress
Tất cả các thành phố có sẵn được hiển thị dưới dạng tùy chọn trong hộp chọn

Trường biểu mẫu thứ hai vẫn là một nút chọn và nó tương ứng với phân loại tùy chỉnh

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
449. Các giá trị của nhóm thứ hai gồm các phần tử
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
477 được cung cấp bởi thẻ mẫu
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
480. Đây là khối mã thứ hai, tạo trường
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
481 mới tương ứng với phân loại
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
449

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
1

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
480 trả về một mảng gồm tất cả các thuật ngữ của phân loại được đặt làm đối số đầu tiên hoặc một đối tượng
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
484 nếu phân loại không tồn tại. Bây giờ, một lần nữa, chu kỳ
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
485 in ra các phần tử tùy chọn

Bộ lọc tìm kiếm tùy chỉnh trong WordPress
Các tùy chọn của hộp chọn thứ hai

Sau đó, chúng tôi xây dựng phần tử

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
481 cuối cùng, tương ứng với trường tùy chỉnh
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
411. Đây là mã

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
2

Như bạn có thể thấy, trong trường hợp này, chúng tôi đã đặt các giá trị tùy chọn theo cách thủ công

Cuối cùng, chúng ta có thể in ra biểu mẫu

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
3

Chúng tôi đã đặt trường nhập ẩn cho truy vấn công khai

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
2 var. Khi người dùng gửi biểu mẫu, WordPress sẽ nhận giá trị
// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
2 và tải kho lưu trữ. php tệp mẫu hoặc, nếu có, tệp lưu trữ-{post_type}. tập tin php. Với loại biểu mẫu này, nếu bạn định tùy chỉnh cấu trúc HTML của trang kết quả, bạn sẽ cần cung cấp tệp mẫu phù hợp nhất

Bộ lọc tìm kiếm tùy chỉnh trong WordPress
Hình ảnh hiển thị biểu mẫu tìm kiếm nâng cao mà chúng tôi đã xây dựng thông qua bài viết này

Tìm kiếm văn bản miễn phí

Biểu mẫu chúng tôi đã tạo cho đến nay cho phép người dùng thiết lập ba bộ lọc từ một số tùy chọn được xác định trước. Bây giờ chúng tôi sẽ cải thiện hệ thống tìm kiếm bao gồm trường văn bản trong biểu mẫu để người dùng có thể tìm kiếm chỗ ở theo từ khóa tùy chỉnh. Chúng ta có thể làm được điều đó nhờ vào

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
45. Vì vậy, hãy thay đổi hình thức như sau

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
4

Nhờ có trường văn bản, chúng ta có thể truyền cho

// An array of arguments
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here
    endwhile;

} else {
        // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
9 một cặp khóa/giá trị mới, trong đó khóa là tham số
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
45 và giá trị là đầu vào của người dùng hoặc giá trị trả về của
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
493. Đọc “Nhận truy vấn tìm kiếm” trong Tham chiếu mã truy vấn WP để biết thêm chi tiết

Bộ lọc tìm kiếm tùy chỉnh trong WordPress
Một hình thức tìm kiếm nâng cao hơn

Một lưu ý cuối cùng. trong ví dụ trước của chúng tôi, chúng tôi đã thấy WordPress tải tệp mẫu lưu trữ để hiển thị kết quả của truy vấn. Đó là bởi vì chúng tôi đã không đặt đối số tìm kiếm. Khi chuỗi truy vấn chứa thông số

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- code here -->
<?php endwhile; else : ?>
    <!-- code here -->
<?php endif; ?>
45, WordPress sẽ tự động tải tệp mẫu tìm kiếm, như thể hiện trong hình ảnh cuối cùng của bài đăng này

Bộ lọc tìm kiếm tùy chỉnh trong WordPress
Hình ảnh hiển thị trang tìm kiếm trong Twenty Fifteen

Phần kết luận

Các ví dụ của bài viết này nhằm chứng minh những gì có thể đạt được với các công cụ do WordPress cung cấp. Chắc chắn, biểu mẫu có thể được cải thiện bằng cách thêm các trường mới cho phép tùy chỉnh chi tiết hơn. Tuy nhiên, tôi hy vọng tôi đã cung cấp một cái nhìn khá chi tiết về các chức năng giúp xây dựng một hệ thống tìm kiếm vượt qua các giới hạn của hệ thống tìm kiếm tích hợp mà không cần các dịch vụ và plugin bên ngoài.