Filtering custom posts with Ajax

As pointed by @mmm, I just needed to change the ‘append()’ for ‘html()’. And the use of ‘-‘ instead of ‘_’ in the ‘action’ part.

I asked him to post it as an answer so I could close the question, but I guess he didn’t see.

So, my final AJAX code is:

$("#opt_filter").change(function () {
var opt_filter = $("#opt_filter").val();
$.ajax({
    type: "POST",
    url: clocal.ajaxurl,
    dataType: "json",
    data: {
        'action': 'load-filter',
        'opt_selected': opt_filter,
    },
    success: function(response) {
        $("#list-of-posts").html(response);
        //return false;
    }
   });
 });

Leave a Comment