To build the search feature in wordpress, I have to say extremely simple, you only need a few small code snippets to do it. But… Search by custom field ? Have you tried If not, today I will show you how to do that function.
To review a bit, I will repeat how to create a normal search form in wordpress. You see the code below dentist
Create a form search in wordpress
The simplest form search
Search by a certain post type
In the example above, I searched for posts belonging to the post type as post. Other post types will not be displayed in the search results
Search by a certain category
The above code allows selecting the category to search.
Search function by custom field in wordpress
To easily imagine I will make a request and then we will follow the request that functions offline, request as follows:
Create real estate search form by keyword and price range. The price range here has two values: the minimum and the largest, with the price of the property stored in the custom field
With the above requirements, we will perform two main jobs: Create a form search and return data processing
Create form search by custom price field:
You insert the following code in the position where you want to display the search form. Note that the code below is just html without any interface, if you want to be beautiful, add css to it.
I will create a form search by the method get, when sumit form the website it will have the following link: domain.com/?post_type=bat-dong-san&s=soil+ background&min-price=500&max-price=50000000
We will rely on the data passed on the url to get the values and process.
Processing search data and display results:
You probably also know the link showing the return data of the search form in the wordpress theme file search.php. So to process the search data we write in the file search.php The following code.
10, 'post_type' => $post_type, 's' => $key, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'price', 'value' => $minprice, 'type' => 'NUMERIC', 'compare' => '>=' ), array( 'key' => 'price', 'value' => $maxprice, 'type' => 'NUMERIC', 'compare' => '<=' ) ), ); ?> have_posts() ): ?> have_posts() ) : $the_query->the_post(); ?>
I will explain in each line for you to follow:
- From line 2 to line 5 perform data checks and retrieve data passed through the get method $ _GET
- Line 7 The number of properties shown is 10
- Line 8 post_type here will get the value ‘bat-dong-san’
- Line 9 search conditions by keyword
- Lines 10 to 25 This is the parameter transmission area Search by custom field
- ‘Relation’ => ‘AND’ The conditions will combine with each other by the equal method
- Lines 12 to 17 condition for minimum value
- Lines 18 to 23 condition for maximum value
- From line 27 to the end is the get post loop in wordpress
Some examples of searching by other custom fields
For example, search for articles with over 100 views
10, 'post_type' => 'post', 'meta_query' => array( array( 'key' => 'views', 'value' => 100, 'type' => 'NUMERIC', 'compare' => '>' ) ) ); $the_query = new WP_Query( $args );?> have_posts() ): ?>
Search 10 products with more than 10 purchases and the color is red.
10, 'post_type' => 'product', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'total_sales', 'value' => 10, 'type' => 'NUMERIC', 'compare' => '>' ), array( 'key' => 'color_product', 'value' => 'red', 'compare' => '=' ) ) ); $the_query = new WP_Query( $args );?> have_posts() ): ?>
Today's article I have introduced you how Search by custom field as well as querying the data related to the custom field. Knowing this dish, you do not have to worry about handling problems in making real estate or travel sites.
This article a bit too much code, you do not understand where can comment below I will support. If possible I will make a video tutorial.
Hello friends! Oh, support the new site learn wordpress his home!
The post Search by custom field in wordpress and its applications appeared first on Blog Huy Kira.Read more :
- The $ 3,500 CME gap is a potential threat to Bitcoin's intense adjustment
- Openpos v4.4.0 - WooCommerce Level Of Sale (POS) Codecanyon
- Cách tắt Turn off Windows Update trên Windows 10
- The most attractive online investment channels 2020
- HTC launches a blockchain router to allow users to run a full Bitcoin node at home