虽然我还没有完全测试过它,但希望它能帮助您实现目标。。。
此处的基本搜索表单将加载site_url()
附加搜索查询字符串。您应该对此进行更改,以更好地反映您的站点布局,例如<?php site_url( \'search\' ); ?>
(\'http://website.com/search\').
将结果页代码放在结果页模板中(基于上面的site\\u url()代码)。这将收集所有部分(输入),将它们转换为字符串,然后使用meta_query
参数
它的基本意思是“查找gc\\U编号等于搜索字符串的所有产品”。然后,您可以循环查看所有结果并输出所需的任何内容。
如果不是你想要的,或者你需要帮助,请在下面发表评论。
<form role="search" method="get" id="acf-search" action="<?php site_url(); ?>">
...
<input type="text" name="part1" />
<input type="text" name="part2" />
<input type="text" name="part3" />
<input type="submit" value="Search" />
</form>
<?php
// Results page
// Might be worth checking to make sure these exist before doing the query.
$part_1 = $_GET[\'part1\'];
$part_2 = $_GET[\'part2\'];
$part_3 = $_GET[\'part3\'];
$search_string = $part_1 . \'-\' . $part_2 . \'-\' . $part_3;
// Get all product categories
$categories = get_terms( \'product_cat\' );
$counter = 0;
// Loop through categories
foreach ( $categories as $category ) {
// Get custom field
$gc_number = get_field( \'gc_number\', $category );
// If field matches search string, echo name
if ( $gc_number == $search_string ) {
// Update counter for use later
$counter++;
echo $category->name;
}
}
// $counter only increases if custom field matches $search_string.
// If still 0 at this point, no matches were found.
if ( $counter == 0 ) {
echo \'Sorry, no results found\';
}
?>