Category Filtering with AJAX error
-
Hi,
I have a list of categories that I am trying to filter my custom post type by, I know there are plugins that do this but would rather not use them.
I am using the following code but am receiving a “POST https://www.goldpine.co.nz/wp-admin/admin-ajax.php 400 (Bad Request):”
Any ideas how to fix this or something in the code I am missing?
<form action=”<?php echo site_url() ?>/wp-admin/admin-ajax.php” method=”POST” id=”filter”>
<?php if( $terms = get_terms( ‘category’, ‘orderby=name’ ) ) :
echo ‘<select name=”categoryfilter”><option>Select category…</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>Apply filter</button>
<input type=”hidden” name=”action” value=”myfilter”>
</form>
<div id=”response”></div><script type=”text/javascript”>
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(‘button’).text(‘Processing…’); // changing the button label
},
success:function(data){
filter.find(‘button’).text(‘Apply filter’); // changing the button label back
$(‘#response’).html(data); // insert data
}
});
return false;
});
});</script>
- The topic ‘Category Filtering with AJAX error’ is closed to new replies.