How to get WordPress post filter (misha_filter_function) to filter CPT categories associated by a tag

I’m using the misha_filter_function from: https://rudrastyh.com/wordpress/ajax-post-filters.html to filter posts.

The initial array is displaying the correct posts by the location_and_season (made with ACF) tag. But something weird is going on with the second array because when I click my dropdown select to sort the posts by category it’s filtering all of the posts within the post type, and effectively ignoring ‘tag’ => $value,

I’m relatively new to WordPress PHP and JS, so my Google-Fu hasn’t been too helpful since I don’t really know what to search. Any help or suggestions would be appreciated. Thanks!

Here’s the filter Code:

<?php
add_action(‘wp_ajax_myfilter’, ‘misha_filter_function’); //
wp_ajax_{ACTION HERE}
add_action(‘wp_ajax_nopriv_myfilter’, ‘misha_filter_function’);

function misha_filter_function()
{
$value = get_field(‘location_and_season’);

$args = array(
‘post_type’ => ‘shows’,
‘tag’ => $value,
‘posts_per_page’ => – 1, // show all posts.
‘orderby’ => ‘name’, // we will sort posts by name
‘order’ => ‘ASC’
//$_POST[‘name’] // ASC or DESC

);

// for taxonomies / categories
// IMPORTANT! Adding && !empty( $_POST[‘categoryfilter’] ) fixes the no posts found for All Categories
if (isset($_POST[‘categoryfilter’]) &&
!empty($_POST[‘categoryfilter’])) $args[‘tax_query’] = array(
array(
‘taxonomy’ => ‘category’,
‘field’ => ‘id’,
‘terms’ => $_POST[‘categoryfilter’]
)
);

$query = new WP_Query($args);
// The Query
query_posts($args);

// The Loop
while (have_posts()):
the_post(); ?>

Here’s the front end dropdown code:

<form action=”<?php echo site_url() ?>/wp-admin/admin-ajax.php”
method=”POST” id=”filter” style=”float:right; margin-right: 15px;”>

<?php
if ($terms = get_terms(array(
‘taxonomy’ => ‘category’,
‘orderby’ => ‘name’
))):

echo ‘<select name=”categoryfilter”><option id=”refresh” value=”all”
class=”dropdown-select”>All Topics…</option>’;

foreach ($terms as $term):
echo ‘<option value=”‘ . $term->term_id . ‘”>’ . $term->name .
‘</option>’;

endforeach;
echo ‘</select>’;
endif;
?>
<div class=”processing” style=”height:30px;”></div>

<input type=”hidden” name=”action” value=”myfilter”>
</form>

And the JS

jQuery(function ($) {
$(‘#filter’).change(function () {
var filter = $(‘#filter’);
$.ajax({
url: filter.attr(‘action’),
data: filter.serialize(), // form data
type: filter.attr(‘method’), // POST
beforeSend: function (xhr) {
filter.find(‘.processing’).html(‘<img src=”spinner.gif” class=”spinner”>’); // changing the button label
},
success: function (data) {
filter.find(‘.processing’).text(”); // changing the button label back
$(‘#response’).html(data); // insert data

}

});

$(document).ready(function () {
$(‘#filter’).on(“click”, function () {
$(‘.post-section’).toggleClass(“active”);
});
});

return false;
});

Read more here:: How to get WordPress post filter (misha_filter_function) to filter CPT categories associated by a tag

Leave a Reply

Your email address will not be published. Required fields are marked *