Last active
November 28, 2020 08:35
-
-
Save Bobz-zg/c2b89c8a7301177c8ab9de75bc50d71e to your computer and use it in GitHub Desktop.
Filter WordPress posts by multiple taxonomy terms with AJAX and pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.sc-ajax-filter-multi').on('click', 'a[data-filter], .pagination a', function(event) { | |
if(event.preventDefault) { event.preventDefault(); } | |
$this = $(this); | |
/** | |
* Set filter active | |
*/ | |
if ($this.data('filter')) { | |
$page = 1; | |
/** | |
* If all terms, then deactivate all other | |
*/ | |
if ($this.data('term') === 'all-terms') { | |
$this.closest('ul').find('.active').removeClass('active'); | |
} | |
else { | |
$('a[data-term="all-terms"]').parent('li').removeClass('active'); | |
} | |
// Toggle current active | |
$this.parent('li').toggleClass('active'); | |
/** | |
* Get All Active Terms | |
*/ | |
$active = {}; | |
$terms = $this.closest('ul').find('.active'); | |
if ($terms.length) { | |
$.each($terms, function(index, term) { | |
$a = $(term).find('a'); | |
$tax = $a.data('filter'); | |
$slug = $a.data('term'); | |
if ($tax in $active) { | |
$active[$tax].push($slug); | |
} | |
else { | |
$active[$tax] = []; | |
$active[$tax].push($slug); | |
} | |
}); | |
} | |
} | |
else { | |
/** | |
* Pagination | |
*/ | |
$page = parseInt($this.attr('href').replace(/\D/g,'')); | |
$this = $('.nav-filter .active a'); | |
} | |
$params = { | |
'page' : $page, | |
'terms' : $active, | |
'qty' : $this.closest('#container-async').data('paged'), | |
}; | |
// Run query | |
get_posts($params); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment