我是一名初级开发人员,目前正在开发一个Wordpress网站,该网站需要手工制作的强大搜索表单。
我有一堆父简单(普通,非自定义)类别(客户、业务部门、运营和业务领域),然后我有一些子类别(例如,客户“测试”是客户的子类别,因此您可以理解我)。然后,我根据他们所属的客户、他们的业务部门、地区等对所有帖子进行分类,因此每个帖子都有自己的类别。
在我的搜索表单中,我在标题中预装了不同的子类别(不需要AJAX),并附加到不同的选择。每个选择选项都有子类别ID的“值”,所以这很好。
问题是,the form can be sent totally EMPTY, searching only for specific words (经典Wordpress搜索),or picking some of the categories from the dropdown (对于WP\\U查询,应该类似于“in\\uu category”(在类别中),or BOTH WORDS AND CATEGORIES.
现在,直截了当地说:我使用WP\\u查询来搜索帖子,首先获取表单发送的参数,然后尝试将参数添加到WP\\u查询,但我没有得到预期的结果。
有一个特别的案例困扰着我:例如,我有一位名叫“星巴克”的顾客。如果我搜索“星巴克”,我会收到大约5-6篇标题中包含“星巴克”的帖子,但如果我搜索“星巴克”,我只会收到一篇使用当前WP\\U查询的帖子。
这是我的高级搜索表单的代码。php
<?php
//Función getSelectItems en functions.php, dado el SLUG de la categoria, obtiene las subcategorias hijas de la categoria del slug.
$sectores = getSelectItems(\'sectores\');
$clientes = getSelectItems(\'clientes\');
$areas = getSelectItems(\'areas-de-especializacion\');
$operaciones = getSelectItems(\'operaciones\');
$titulos = getAllPostTitles();
?>
<form method="post" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<!-- PASSING THIS TO TRIGGER THE ADVANCED SEARCH RESULT PAGE FROM functions.php -->
<input type="hidden" name="search" value="advanced">
<input type="search" id="acexito" class="acexito search-field" name="s" placeholder="<?php _e( \'¿qué estás buscando?...\', \'textdomain\' ); ?>" value="<?php get_search_query(); ?>"/>
<button type="button" id="btnfiltros" class="btnfiltros btnclaro"></button>
<div id="filtros" class="filtros">
<div class="rowfilter">
<label for="area" class="leftinput"><?php _e( \'Área\', \'textdomain\' ); ?></label>
<select class="iptfiltro" name="area[]" id="area" multiple="multiple">
<?php
for($i=0;$i<count($areas);$i++){
?>
<option value="<?php echo $areas[$i][\'id\'];?>"><?php echo $areas[$i][\'name\'];?></option>
<?php
}
?>
</select>
</div>
<div id="rowcliente" class="rowfilter">
<label for="cliente" class="leftinput"><?php _e( \'Cliente\', \'textdomain\' ); ?></label>
<select class="iptfiltro" name="cliente[]" id="cliente" multiple="multiple">
<?php
for($i=0;$i<count($clientes);$i++){
?>
<option value="<?php echo $clientes[$i][\'id\'];?>"><?php echo $clientes[$i][\'name\'];?></option>
<?php
}
?>
</select>
</div>
<div id="rowsector" class="rowfilter">
<label for="sector" class="leftinput"><?php _e( \'Sector\', \'textdomain\' ); ?></label>
<!--<input type="text" name="sector" id="sector" class="iptfiltro"/>-->
<select class="iptfiltro" name="sector[]" id="sector" multiple="multiple">
<?php
for($i=0;$i<count($sectores);$i++){
?>
<option value="<?php echo $sectores[$i][\'id\'];?>"><?php echo $sectores[$i][\'name\'];?></option>
<?php
}
?>
</select>
</div>
<div class="rowfilter">
<label for="operacion" class="leftinput"><?php _e( \'Operación\', \'textdomain\' ); ?></label>
<select class="iptfiltro" name="operacion[]" id="operacion" multiple="multiple">
<?php
for($i=0;$i<count($operaciones);$i++){
?>
<option value="<?php echo $operaciones[$i][\'id\'];?>"><?php echo $operaciones[$i][\'name\'];?></option>
<?php
}
?>
</select>
</div>
<div class="rowfilter fecha" id="rowini">
<label for="fechaini" id="labelfecha" class="leftinput"><?php _e( \'Fecha\', \'textdomain\' ); ?></label>
<!--<label id="labelini" for="fechaini" class="leftinput"><?php //_e( \'Inicio\', \'textdomain\' ); ?></label>-->
<input type="text" name="fechaini" id="fechaini" class="iptfiltro" placeholder="<?php _e( \'Inicio\', \'textdomain\' ); ?>"/>
<!--<label id="labelfin" class="leftinput"><?php //_e( \'Fin\', \'textdomain\' ); ?></label>-->
<input type="text" name="fechafin" id="fechafin" class="iptfiltro" placeholder="<?php _e( \'Fin\', \'textdomain\' ); ?>"/>
</div>
</div>
<input type="submit" id="submitsearch" class="submitsearch" value="Buscar"/>
</form>
然后,这里是我的函数的内容。php文件
//Obtain categories for preloading
function getSelectItems($slugcategoria){
$categoria = get_category_by_slug($slugcategoria);
$args = array(
\'child_of\' => $categoria->term_id,
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => FALSE,
\'hierarchical\' => 1,
\'taxonomy\' => \'category\'
);
$child_categories = get_categories($args);
$items = array();
foreach($child_categories as $cat){
$desglose = array(
\'id\' => $cat->cat_ID,
\'name\' => $cat->name
);
array_push($items, $desglose);
}
return $items;
}
function customSearchByParams($keywords,$clientes,$areas,$sectores,$operaciones,$fechaini,$fechafin){
//Crear array de IDs de categoria previamente
//Wordpress - fecha formato Y-m-d
$cats = array();
//Add "customers" to the query
if(!empty($clientes)){
for($i=0;$i<count($clientes);$i++){
array_push($cats, $clientes[$i]);
}
}
//Add "business areas" to the query
if(!empty($areas)){
for($i=0;$i<count($areas);$i++){
array_push($cats, $areas[$i]);
}
}
//Add "business sectors" to the query
if(!empty($sectores)){
for($i=0;$i<count($sectores);$i++){
array_push($cats, $sectores[$i]);
}
}
//Add items from "operation" subcategories
if(!empty($operaciones)){
for($i=0;$i<count($operaciones);$i++){
array_push($cats, $operaciones[$i]);
}
}
/*I check for "--" because my datepicker library sends "--" if nothing was selected*/
//Filter by date
if($fechaini=="--" && $fechafin=="--"){
//No dates
$args = array(
\'category__in\' => $cats
);
}else if($fechaini!="--" && $fechafin=="--"){
//Only posts after given date
$partsini = explode("-", $fechaini);
$args = array(
\'category__in\' => $cats,
\'date_query\' => array(
array(
\'after\' => array(
\'year\' => $partsini[0],
\'month\' => $partsini[1],
\'day\' => $partsini[2],
),
\'inclusive\' => true
)
)
);
}else if($fechaini=="--" && $fechafin!="--"){
//Only posts before given date
$partsfin = explode("-", $fechafin);
$args = array(
\'category__in\' => $cats,
\'date_query\' => array(
array(
\'before\' => array(
\'year\' => $partsfin[0],
\'month\' => $partsfin[1],
\'day\' => $partsfin[2],
),
\'inclusive\' => true
)
)
);
}else{
//Both (date in between two given dates)
$partsini = explode("-", $fechaini);
$partsfin = explode("-", $fechafin);
$args = array(
\'category__in\' => $cats,
\'date_query\' => array(
array(
\'after\' => array(
\'year\' => $partsini[0],
\'month\' => $partsini[1],
\'day\' => $partsini[2],
),
\'before\' => array(
\'year\' => $partsfin[0],
\'month\' => $partsfin[1],
\'day\' => $partsfin[2],
),
\'inclusive\' => true,
\'relation\' => \'AND\'
)
)
);
}
if(!empty($keywords)){
$args[\'s\'] = $keywords;
$words = explode(" ", $keywords);
foreach($words as $word){
$slug = slugify($word);
$categoria = get_category_by_slug($slug);
if(!empty($categoria)){
array_push($cats, $categoria->cat_ID);
}
}
$args[\'category__in\'] = $cats;
}
$args[\'posts_per_page\'] = 10;
$args[\'nopaging\'] = false;
$args[\'paged\'] = false;
$the_query = new WP_Query($args);
return $the_query;
}
最后一个函数阐述了我稍后使用的WP\\u查询,并相应地使用模板输出。我想修复的是查询,基本上,我需要它变得智能和强大。
我已经阅读了文档,并亲自尝试了一些东西。我想坚持使用WP\\u查询,因为我完全不知道WP数据库本身,我想避免使用纯MySQL查询来获取帖子。
我怎样才能让它工作?我目前的版本可以工作,但我认为可以做得更好。如有任何建议,我们将不胜感激。