Skip to content

Instantly share code, notes, and snippets.

@Bobz-zg
Last active October 12, 2016 14:16
Show Gist options
  • Save Bobz-zg/72c2e6b2e6c6c27cb8c66d90bc60181e to your computer and use it in GitHub Desktop.
Save Bobz-zg/72c2e6b2e6c6c27cb8c66d90bc60181e to your computer and use it in GitHub Desktop.
Filter WordPress posts by multiple taxonomy terms with AJAX and pagination
<?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