Simple AJAX posts filter by category and publish date for WordPress

The first thing you need to do is to write some HTML for category and publish date select box. This can be added to a custom template or your home page index.php file.

Now, when we have HTML ready, we also need to show some latest blog posts.

Create a file called content-post.php in template-parts directory. If this directory doesn’t exist, then create it. We’ll be showing the title of the post and linking to it.

Add this JavaScript code to your theme’s javascript file. This code filters out the posts by category and publish date under AJAX. The request is sent to ajax_filterposts_handler PHP function, then response is delivered by JavaScript (jQuery).

Finally, the PHP part. Add this to your functions.php file.

Join the Conversation

3 Comments

  1. Hey,
    I did this for custom taxonomy with custom post type
    ‘categorie’,
    ‘orderby’ => ‘name’,
    ‘order’ => ‘ASC’,
    ‘hide_empty’ => 0
    );

    $get_categories = get_categories($args); ?>
    but its not working it filter all posts every time.

  2. hi, i am trying your method of filtering posts, when i filter it shows loading and nothing happen area goes blank

Leave a comment

Your email address will not be published. Required fields are marked *