我想做的是使用搜索表单中的元键查询 WordPress 自定义帖子类型。搜索表单接受用户输入并根据匹配条件显示结果,表单的某些字段可能为空白,因此我需要确保没有将任何空白值传递给查询参数。我需要在参数数组中使用 if 。
我将不胜感激任何类型的帮助。
这是我正在使用的代码,但收到错误消息。
Parse error: syntax error, unexpected T_IF, expecting ')'
这是我的代码:
if (isset($POST['stock']) && !empty($POST['stock'])) {
$stock = $POST['stock'];
}
if (isset($POST['mineral']) && !empty($POST['mineral'])) {
$mineral = $POST['mineral'];
}
if (isset($POST['species']) && !empty($POST['species'])) {
$species = $POST['species'];
}
if (isset($POST['color']) && !empty($POST['color'])) {
$color = $POST['color'];
}
if (isset($POST['chemicalclass']) && !empty($POST['chemicalclass'])) {
$chemicalclass = $POST['chemicalclass'];
}
if (isset($POST['locality']) && !empty($POST['locality'])) {
$locality = $POST['locality'];
}
if (isset($POST['description']) && !empty($POST['description'])) {
$description = $POST['description'];
}
if (isset($POST['size']) && !empty($POST['size'])) {
$size = $POST['size'];
}
if (isset($POST['pricegt'])) {
$pricegt = $POST['pricegt'];
} else {
$pricegt = 0;
}
if (isset($POST['pricelt'])) {
$pricelt = $POST['pricelt'];
} else {
$pricelt = 999999;
}
$args = array(
'post_type' => 'products',
'productspecies' => $species,
'localities' => $locality,
'meta_query' => array(
//'relation' => 'OR',
array(
'key' => '_price',
'value' => array( $pricegt, $pricelt ),
'compare' => 'BETWEEN',
'type' => 'numeric',
),
if ($mineral) {
array(
'key' => '_components',
'value' => $mineral,
),
}
if ($stock) {
array(
'key' => '_lotnum',
'value' => $stock,
),
}
if ($color) {
array(
'key' => '_color',
'value' => $color,
),
}
if ($description) {
array(
'key' => '_smalldesc',
'value' => $description,
'compare' => 'LIKE',
),
}
if ($size) {
array(
'key' => '_size',
'value' => $size,
),
}
if ($chemicalclass) {
array(
'key' => '_chemicalclass',
'valeu' => $chemicalclass,
),
}
),
);
?>
<?php $query = new WP_Query( $args ); ?>
<div class="postcount">We Found A Total Of <span><?php echo $query->post_count;?></span> Items Maching Your Search</div>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
我做错了什么?