好的,我正在为客户端创建一个自定义搜索,该搜索接受下拉输入,然后显示符合该标准的所有产品。
我已经让它工作了,如果你输入一个特定产品的信息,它会在结果中返回那个产品。我想让它做它所做的,但我也希望它返回部分匹配,如果下拉菜单没有被选中。
现在如果你选择四个下拉列表中的两个,它不会返回任何结果。我希望它根据所选择的内容返回结果,无论选择哪个下拉菜单或选择了多少个。
示例如下:http://carotape.bigwolfdesigns.com/product-selector/
代码在这里:
<?if ( !$_GET['submit'] ) : ?>
<form method="get">
<?php
echo "<br><strong>Thickness:</strong><br>";
product_selector('milThickness_box', 'thickness');
echo "<br><strong>Adhesion:</strong><br>";
product_selector('adhesion_box', 'adhesion');
echo "<br><strong>Substrate:</strong><br>";
product_selector('substrate_box', 'substrate');
echo "<br><strong>Elongation:</strong><br>";
product_selector('elongation_box', 'elongation');
?>
<br>
<input type="submit" name="submit" value="submit">
</form>
<?php else :
$ad = array( 'key' => 'adhesion_box', 'value' => $_GET['adhesion'] );
$el = array( 'key' => 'elongation_box', 'value' => $_GET['elongation'] );
$th = array( 'key' => 'milThickness_box', 'value' => $_GET['thickness'] );
$su = array( 'key' => 'substrate_box', 'value' => $_GET['substrate'] );
$args = array(
'post_type' => 'post',
'meta_query' => array($ad, $el, $th, $su),
);
$myQuery = new WP_Query($args); ?>
<ul>
<?php
while ($myQuery->have_posts()) :
$myQuery->the_post(); ?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
product_selector();
函数简单地接受输入变量,并使用它将特定的meta_key读取到foreach()
的下拉列表中。
function product_selector($var, $n) {
//Declare Globals
global $wpdb;
// set the meta_key to the appropriate custom field meta key
$a = $wpdb->get_col( $wpdb->prepare(
"
SELECT meta_value
FROM $wpdb->postmeta
WHERE meta_key = %s
",
$var
) );
//Remove duplicate values.
$b = array_unique($a);
//Remove null values.
$c = array_filter($b);
//Sort the array
$d = sort($c);
// Print our form.
echo "<select name='"$n'">";
echo "<option value='"'">Please Choose</option>";
foreach ( $c as $k => $v ) :
echo "<option value='"$v'">$v</option>";
endforeach;
echo "</select>";
}
首先检查$_GET[]
是否为空,然后将值分配给数组。
if( !empty($_GET['adhesion']) {
$ad = array( 'key' => 'adhesion_box', 'value' => $_GET['adhesion'] );
}