Last active
October 12, 2016 14:16
-
-
Save Bobz-zg/72c2e6b2e6c6c27cb8c66d90bc60181e to your computer and use it in GitHub Desktop.
Filter WordPress posts by multiple taxonomy terms with AJAX and pagination
This file contains hidden or 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
<?php | |
/** | |
* Check if term exists | |
*/ | |
if (!is_array($terms)) : | |
$response = [ | |
'status' => 501, | |
'message' => 'Term doesn\'t exist', | |
'content' => 0 | |
]; | |
die(json_encode($response)); | |
else : | |
foreach ($terms as $tax => $slugs) : | |
if (in_array('all-terms', $slugs)) { | |
$all = true; | |
} | |
$tax_qry[] = [ | |
'taxonomy' => $tax, | |
'field' => 'slug', | |
'terms' => $slugs, | |
]; | |
endforeach; | |
endif; | |
/** | |
* Setup query | |
*/ | |
$args = [ | |
'paged' => $page, | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => $qty, | |
]; | |
if ($tax_qry && !$all) : | |
$args['tax_query'] = $tax_qry; | |
endif; | |
$qry = new WP_Query($args); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment