Skip to content

Instantly share code, notes, and snippets.

@Bobz-zg
Created August 25, 2016 10:06
Show Gist options
  • Save Bobz-zg/4f41fa30fc28b0ffd3d91ac5bacb079b to your computer and use it in GitHub Desktop.
Save Bobz-zg/4f41fa30fc28b0ffd3d91ac5bacb079b to your computer and use it in GitHub Desktop.
Filter WordPress posts by custom taxonomy term with AJAX
function get_posts($params) {
$container = $('#container-async');
$content = $container.find('.content');
$status = $container.find('.status');
$status.text('Loading posts ...');
$.ajax({
url: bobz.ajax_url,
data: {
action: 'do_filter_posts',
nonce: bobz.nonce,
params: $params
},
type: 'post',
dataType: 'json',
success: function(data, textStatus, XMLHttpRequest) {
if (data.status === 200) {
$content.html(data.content);
}
else if (data.status === 201) {
$content.html(data.message);
}
else {
$status.html(data.message);
}
},
error: function(MLHttpRequest, textStatus, errorThrown) {
$status.html(textStatus);
/*console.log(MLHttpRequest);
console.log(textStatus);
console.log(errorThrown);*/
},
complete: function(data, textStatus) {
msg = textStatus;
if (textStatus === 'success') {
msg = data.responseJSON.found;
}
$status.text('Posts found: ' + msg);
/*console.log(data);
console.log(textStatus);*/
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment