问题描述
好的,我正在为接受下拉输入的客户创建自定义搜索,然后显示符合该条件的所有产品.
Okay so I'm creating a custom search for a client that takes drop-down input and then shows all the products that meet that criteria.
我已经让它工作了,如果您输入特定产品的信息,它只会在结果中返回该产品.我希望它完全按照它所做的去做,但我也希望它在未选择下拉列表的情况下返回部分匹配项.
I've got it working in that if you put in information for a specific product, it will return just that product in the results. I want it to do exactly what it does, but I also want it to return partial matches if drop-downs aren't selected.
现在,如果您选择四个下拉菜单中的两个,它不会返回任何结果.我希望它根据选择的内容返回结果,无论选择哪个下拉菜单或选择了多少个.
Right now if you select two of the four drop downs, it doesn't return any results. I want it to return results based on what was selected, no matter which drop down or how many are selected.
示例如下: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();
函数简单地获取输入变量,并使用它通过 foreach()
将特定的 meta_key 读取到下拉列表中.
The product_selector();
function simply takes the input variable and uses it to read the specific meta_key into drop downs with 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[]
是否为空,然后将值赋值给数组.
First you have to check whether $_GET[]
is empty or not then assign the values to an array.
if( !empty($_GET['adhesion']) {
$ad = array( 'key' => 'adhesion_box', 'value' => $_GET['adhesion'] );
}
这篇关于如果空白字段,Wordpress 在搜索中包含所有元值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!