nach langem recherchieren bin ich auf das gestoßen: https://rudrastyh.com/wordpress/ajax-post-filters.html
Aber ich kann es nur zum Laufen bringen, wenn zwei Optionen ausgewählt sind.
Ich gebe Ihnen etwas Kontext: Ich habe ein CPT namens „Contratistas“, das zwei benutzerdefinierte Taxonomien „especialidad“ und „industria“ hat, beide haben jeweils zwei Begriffe „especialidad“ -> „tecnologia“ und „auditoria“; ‘industria’ -> ‘cultivo’ und ‘depocito’
hier ist meine funktion:
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']
);
}
Es funktioniert, wenn Sie etwas aus beiden Taxonomien auswählen, aber wenn eine leer ist, heißt es: “Es gibt keinen Beitrag”.
Als Bonus möchte ich alle Beiträge vor dem Filtern anzeigen.
Ich hoffe jemand kann mir helfen! Danke! Ich bin ziemlich neu bei WordPress
BEARBEITEN! Hier ist mein js und mein Formular, ich bin hier ratlos, ich kann nicht herausfinden, was falsch ist 🙁
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;
});
meine php-datei:
<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>