经过大量研究后,我发现了这个:https://rudrastyh.com/wordpress/ajax-post-filters.html
但是只有当选择了两个选项时我才能让它工作。
我会给你一些背景信息: 我有一个名为“Contratistas”的 CPT,它有两个自定义分类法“especialidad”和“industria”,它们都有两个术语,每个“especialidad”->“tecnologia”和“auditoria”; '工业' -> 'cultivo' 和 'depocito'
这是我的功能:
function misha_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'], // ASC or DESC
'post_per_page' => -1,
'post_type' => 'contratista'
);
// for taxonomies / categories
if( isset( $_POST['filtroEspecialidad'] ) && isset ($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
//'relation' => 'AND',
array(
'taxonomy' => 'especialidad',
'field' => 'id',
'terms' => $_POST['filtroEspecialidad']
),
array(
'taxonomy' => 'industria',
'field' => 'id',
'terms' => $_POST['filtroIndustria']
),
);
} elseif( !isset($_POST['filtroEspecialidad'] ) && isset($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
'taxonomy' => 'industria',
'field' => 'id',
'terms' => $_POST['filtroIndustria']
);
} elseif( isset( $_POST['filtroEspecialidad'] ) && !isset($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
'taxonomy' => 'especialidad',
'field' => 'id',
'terms' => $_POST['filtroEspecialidad']
);
}
如果您从两个分类法中选择某些内容,它就会起作用,但是当一个分类法为空时,它会显示“没有帖子”
作为奖励,我想在过滤之前显示所有帖子。
我希望有人能帮助我!谢谢!我对 WordPress 还很陌生
编辑!这是我的js和我的表单,我在这里不知所措,我不知道出了什么问题:(
jQuery(function($){
$('#filter').submit(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
filter.find('.filtrar').text('Procesando...'); // changing the button label
},
success:function(data){
filter.find('.filtrar').text('Filtrar'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
});
我的PHP文件:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<div class="titulo mb-3">
<h3>Especialidad</h3>
</div>
<?php
if( $terms = get_terms( array( 'taxonomy' => 'especialidad', 'orderby' => 'name' ) ) ) :
echo '<select name="filtroEspecialidad"><option value="">Seleccione una especialidad...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<div class="titulo my-3">
<h3>Industrias</h3>
</div>
<?php
if( $terms = get_terms( array( 'taxonomy' => 'industria', 'orderby' => 'name' ) ) ) :
echo '<select name="filtroIndustria"><option value="">Seleccione una industria...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<button class="my-3 filtrar">Filtrar</button>
<a href="" id="clear">Clear</a>
<input type="hidden" name="action" value="myfilter">
</form>
您可能想再看一下 WP_Query()
id
参数名称在上下文中有点松散。我也不完全确定您最初的“两者”是否按照您的预期工作,因为 'field' => 'id'
实际上是无效的。
来自文档:
field(字符串)- 选择分类术语。可能的值为
、term_id
、name
或slug
。默认值为term_taxonomy_id
。term_id
id
实际上不是一个有效的选项。如果您只是使用 term_id
,则应该可以省略它。您还可以根据是否设置了过滤器以编程方式添加 tax_query
数组参数,而不是检查“both set
、this set/that unset
、this unset, that set
”,也许是这样的?
function misha_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'], // ASC or DESC
'post_per_page' => -1,
'post_type' => 'contratista'
);
if( isset($_POST['filtroEspecialidad']) || isset($_POST['filtroIndustria']) ){
$args['tax_query'] = array();
if( isset($_POST['filtroEspecialidad']) ){
$args['tax_query'][] = array(
'taxonomy' => 'especialidad',
'terms' => $_POST['filtroEspecialidad']
);
}
if( isset($_POST['filtroIndustria']) ){
$args['tax_query'][] = array(
'taxonomy' => 'industria',
'terms' => $_POST['filtroIndustria']
);
}
if( count($args['tax_query']) > 1 ){
$args['tax_query']['relation'] = 'AND';
}
}
// Run WP_Query with new $args, etc.
}
我不确定
$_POST
值是数组还是单个数字,但如果您使用的是用户提供的输入,您可能需要使用 array_map
和/或 absint
来验证这些值,但如果它们是只是 ID,以上现在应该可以工作。