我想要做的是使用搜索表單中的元鍵查詢WordPress自定義帖子類型。搜索表單接受用戶輸入並根據匹配條件顯示結果,表單的某些字段可能爲空,所以我需要確保不會將任何空值傳遞給查詢參數。我需要使用如果在參數數組中。如果嵌套數組中的語句
我會很感激任何類型的幫助。
這是我正在使用的代碼,但收到錯誤消息。
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(); ?>
我做錯了嗎?
你使用這些'if'內數組? – 2013-07-19 13:58:28
哪一行?在<?php $ query = new WP_Query($ args);之前嘗試刪除最後一個)。 ?> –
題外話:不應該'$ POST'是'$ _POST'? –