php - 使用多个键查询帖子过滤
问题描述
您好,我正在开发一个房地产网站,我希望用户能够按位置进行过滤。我有一个文本输入供用户执行此操作。在后端,我有三个高级自定义字段,例如每个指定位置的不同部分;镇、县和邮政编码。现在我需要用户能够在一个输入框中输入城镇、县或邮政编码,然后我想存储该值并使用它来检查所有字段。我尝试了两种不起作用的方法;
尝试一:
<?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>
解决方案
您可以使用“关系”选项来确定如何构建查询。此外,meta_query 子句可以嵌套。在您的第一个示例中,查询可能是这样的:
<?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(
'relation' => 'AND',
array(
'key' => 'property_status',
'value' => 'For Sale'
),
array(
'relation' => 'OR',
array(
'key' => 'town',
'value' => $location,
'compare' => 'LIKE'
),
array(
'key' => 'county',
'value' => $location,
'compare' => 'LIKE'
),
array(
'key' => 'postcode',
'value' => $location,
'compare' => 'LIKE'
)
)
)
));
在这个查询中,我们要使用 property_status = For Sale AND (town OR County OR postcode) = $location 的帖子。
您可以在此处的“多个自定义字段处理”部分中找到更多详细信息: https ://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
推荐阅读
- javascript - 推荐技术/语言(或推荐的 SAAS)以构建全栈 Web 应用程序
- django - 如何在 Django 中使用关系
- python - tkinter 从其他文件中的子函数获取变量到我的 gui?
- spring-cloud-sleuth - 我可以使用 Spring Cloud Sleuth 根据消息头值将标签添加到消息传递范围吗?
- python - InvalidArgumentError:节点 while_1/Merge_1 的输入 1 是从 while_1/NextIteration_1:0 传递的浮点数
- spring - SpringBoot 和清理@PathVariable
- elasticsearch - 如何在 ElasticSearch 查询中实现“小写”?
- pandas - 如何使用 MultiLabelBinarizer 进行多标签分类?
- html - 没有 PWA 的 iOS Safari 状态栏主题颜色
- time-complexity - 显示这两个函数中哪个增长得更快?