Ajax wordpress 过滤器
Ajax filter for wordpress
经过大量研究,我发现了这个:https://rudrastyh.com/wordpress/ajax-post-filters.html
但只有在有两个选项时我才能让它工作 selected.
我会给你一些背景信息:
我有一个名为 'Contratistas' 的 CPT,它有两个自定义分类法 'especialidad' 和 'industria',它们都有两个术语 'especialidad' -> 'tecnologia' 和 'auditoria'; 'industria' -> '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']
);
}
如果你 select 来自两个分类法的东西,它会起作用,但是当一个为空时,它会说 'there are no post'
作为奖励,我想在过滤前显示所有 post。
希望有人能帮助我!谢谢!我是 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>
您可能想再看 Taxonomy Parameters of WP_Query()
一眼。不幸的是,WordPress 的 id
参数名称在上下文中有点松散。我不完全确定你最初的“两者”是否像你想要的那样工作,因为 'field' => 'id'
实际上是无效的。
来自文档:
field (string) – Select taxonomy term by. Possible values are term_id
, name
, slug
or term_taxonomy_id
. Default value is 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
and/or absint
验证这些值,如果您重新使用用户提供的输入,但如果它们只是 ID,以上内容现在应该可以使用。
经过大量研究,我发现了这个:https://rudrastyh.com/wordpress/ajax-post-filters.html
但只有在有两个选项时我才能让它工作 selected.
我会给你一些背景信息: 我有一个名为 'Contratistas' 的 CPT,它有两个自定义分类法 'especialidad' 和 'industria',它们都有两个术语 'especialidad' -> 'tecnologia' 和 'auditoria'; 'industria' -> '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']
);
}
如果你 select 来自两个分类法的东西,它会起作用,但是当一个为空时,它会说 'there are no post'
作为奖励,我想在过滤前显示所有 post。
希望有人能帮助我!谢谢!我是 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>
您可能想再看 Taxonomy Parameters of WP_Query()
一眼。不幸的是,WordPress 的 id
参数名称在上下文中有点松散。我不完全确定你最初的“两者”是否像你想要的那样工作,因为 'field' => 'id'
实际上是无效的。
来自文档:
field (string) – Select taxonomy term by. Possible values are
term_id
,name
,slug
orterm_taxonomy_id
. Default value isterm_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
and/or absint
验证这些值,如果您重新使用用户提供的输入,但如果它们只是 ID,以上内容现在应该可以使用。