你好,我在一个物业网站工作,我希望用户能够按位置过滤。我有一个文本输入供用户执行此操作。在后端,我有三个高级自定义字段,每个字段指定位置的不同部分,例如;镇、县和邮政编码。现在,我需要用户能够在一个输入框中输入城镇、县或邮政编码,然后我想存储该值并使用它检查所有字段。我试过两种方法,但都不管用;
尝试一次:
<?php
if($_GET['min_price'] && !empty($_GET['min_price'])){
$min_price = $_GET['min_price'];
}else{
$min_price = 0;
}
if($_GET['max_price'] && !empty($_GET['max_price'])){
$max_price = $_GET['max_price'];
}else{
$max_price = 10000000;
}
if($_GET['bedrooms'] && !empty($_GET['bedrooms'])){
$bedrooms = $_GET['bedrooms'];
}
if($_GET['location'] && !empty($_GET['location'])){
$location = $_GET['location'];
}
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'property',
'orderby' => 'date',
'meta_query' => array(
array(
'key' => 'property_status',
'value' => 'For Sale'
),
array(
'key' => 'town',
'value' => $location,
'compare' => 'LIKE'
),
array(
'key' => 'county',
'value' => $location,
'compare' => 'LIKE'
),
array(
'key' => 'postcode',
'value' => $location,
'compare' => 'LIKE'
)
)
));
尝试二:
<?php
if($_GET['min_price'] && !empty($_GET['min_price'])){
$min_price = $_GET['min_price'];
}else{
$min_price = 0;
}
if($_GET['max_price'] && !empty($_GET['max_price'])){
$max_price = $_GET['max_price'];
}else{
$max_price = 10000000;
}
if($_GET['bedrooms'] && !empty($_GET['bedrooms'])){
$bedrooms = $_GET['bedrooms'];
}
if($_GET['location'] && !empty($_GET['location'])){
$location = $_GET['location'];
}
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'property',
'orderby' => 'date',
'meta_query' => array(
array(
'key' => 'property_status',
'value' => 'For Sale'
),
array(
'key' => 'property_price',
'type' => 'NUMERIC',
'value' => array($min_price, $max_price),
'compare' => 'BETWEEN'
),
array(
'key' => 'bedrooms',
'value' => $bedrooms,
'compare' => 'LIKE'
),
array(
'key' => array('town', 'county', 'postcode'),
'value' => $location,
'compare' => 'LIKE'
)
)
));
html:
<form action="<?php the_permalink(); ?>" method="get">
<label>Min:</label>
<input type="number" name="min_price"><br>
<label>Max:</label>
<input type="number" name="max_price"><br>
<label>Bedrooms:</label><br>
<label>1</label><input type="radio" name="bedrooms" value="1">
<label>2</label><input type="radio" name="bedrooms" value="2">
<label>3</label><input type="radio" name="bedrooms" value="3">
<label>4</label><input type="radio" name="bedrooms" value="4">
<label>5</label><input type="radio" name="bedrooms" value="5">
<label>6+</label><input type="radio" name="bedrooms" value="6+">
<label>Location</label><br>
<input type="text" name="location">
<input type="submit">
</form>