What I'm trying to achieve is to enable users to filter posts based on the selection of the checkboxes located on the sidebar.

I've created a custom post type called projects and a couple of taxonomies such as clients and expertises. I found this which is exactly what I want to my page to look like with AJAX. However, I think there are some missing code in there. Anyway, I have managed to display all of taxonomies out with checkboxes without any problems.

            <!-- Show clients taxonomies in a checkbox -->
            <?php foreach( $myClient as $a ) { ?>
                <div id="cat_id">
                    <input class="client_filter" name="<?php echo $a->term_id; ?>" type="checkbox"><?php echo " ".$a->cat_name; ?>

            <?php } ?>

            <!-- show expertises taxonomies in a checkbox -->
            <?php foreach( $expertStyles as $b ) { ?>
                    <input class="expertise_filter" name="<?php echo $b->term_id; ?>" type="checkbox"><?php echo " ".$b->cat_name; ?>
            <?php } ?>

In my script.js, I have the code below:

jQuery(document).ready(function($) {

/* This function will be called on click event */   
var optionsChecked = function() {

/* initialize all the variables */
    var clients = [];
var expertises = [];
var term_id = jQuery('#cat_id').text();

/* loop and push any checked checkboxes into empty array */
jQuery(".client_filter input:checked").each(function() {
    var client_id = jQuery(this).attr('name');

jQuery(".expertise_filter input:checked").each(function() {
    var expertise_id = jQuery(this).attr('name');

/* collect all the data */
var myData = {
    'term_id': term_id,
    'client': clients,
    'expertise': expertises

/* Ajax url */
var url = "/mysite/ajax";

jQuery.post( url, myData, function( myData ){
    jQuery('.filter-result-form').append( myData );


$( "input[type='checkbox']" ).on("click", optionsChecked);


When I run this code, I see nothing come up in the filter result form. I must admit that I'm still learning about jQuery and AJAX.

1 Answer 1


Please read the Wordpress tutorial on using AJAX in plugins (you are basically adding a plugin to your theme if you need to use AJAX). There are two things I see right off the bat that need to be fixed before your proceed. Note what the tutorial says about the global variable ajaxurl. Also make sure you have set up a php function to process the AJAX request, and that you specify that function in the action field of your data. It's like you're making a phone call without a phone number; the system doesn't know where to send the AJAX request for processing.

I'd work on anything the tutorial suggests, but I think those two things are the big ones right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.