WordPress post sorting with AJAX

So This is long question so giving you a simple example that will work fo
Step 1) Cat1 cat2 cat3
for this first you need to call custom taxonomy in a form

Step 2) Assume you have cat1 cat2 as an checkbox with value of custom taxonomy id

Step 3) Add a click event

Step 4) Call your ajax function

Goto this example – http://www.tinyjewelbox.com/product-category/jewelry/ – I hope you wanted to achieve this click on checkboxes are custom taxonomy and on click of filter sorting according to taxonomy.

Method – lets start : –

This is the output template page_lat.php :

Search Results (Currently only prints title and post type :

            <?php 
            if($_GET['s'] && !empty($_GET['s'])){
              $text =$_GET['s'];
              $posttype = $_GET['post_type'];

            }
            else{
                $posttype="custom_post_type";
            }
            ?>
            //This will fetch your custom taxonomy cat1, cat2 , cat3

            <form action="" method="" class="" id="filter-menu" >
            <?php 
            $terms = get_terms('post_tag',, array('hide_empty' => false) );
            foreach($terms as $filteroption)
            {
                <input type="checkbox"  name="filter[]" value="<?php echo $filteroption->term_id; ?>" onclick="filtermenu()" >  
                <?php echo $filteroption->name; ?>
            }
            <input type="hidden" name="posttype" id="posttype" value="<?php echo $posttype; ?>" />
            ?>
            <form>
             <div class="container">
                 <div class="load_html">

                      <?php
                        $args = array(
                          'post_per_page'=> -1,
                          's'=>$text
                        );

                        $query= new WP_Query($args);
                        while($query->have_posts()): $query->the_post();
                      ?>

                      <div>
                        <h5><?php the_title(); ?></h5>
                        <strong>
                          <?php echo get_post_type(); ?>
                        </strong>
                      </div>
                    <?php endwhile; wp_reset_query(); ?>
                  </div>
              </div>

Add click event on click

                <script> //this a click event of checkbox which call ajax action
            function filtermenu(paged)
            {   
            var filtercheckedbox = // get your checkbox value via jquery; 
            var posttype = // get your posttype value; jQuery('#posttype').val(0);
            jQuery.ajax({
                url: AJAXURL,
                data: {
                    'action' : 'filter_menu' ,
                    'checkbox': filtercheckedbox, 
                    'posttype' :posttype,
                    'paged' : 1, 
                },
                type: 'POST',
                success: function(data) {
                        jQuery(".load_html").html(data);    
                }
            });     

            }   
            </script>

Add your action into functions.php

            <?php 
            // add this function to functions.php this is your ajax action 
            add_action( 'wp_ajax_filter_menu', 'wp_ajax_filter_menu' );
            add_action( 'wp_ajax_nopriv_filter_menu', 'wp_ajax_filter_menu' );

            function wp_ajax_filter_menu()
            {
            global $post, $wp_query;

            $args = array(
                'post_type' => '$_POST['post_type']',
                'tax_query' => array(
                                'relation' => 'AND',
                                 array(
                                    'taxonomy' => 'custom_taxonomy name',
                                    'field'    => 'term_id',
                                    'terms'    => array($_POST['checkbox']),
                                 ),
                ),
                'orderby'   => 'ASC',                   
            );

                $filteroption = new WP_Query( $args );
                if ( $filteroption->have_posts() ) :
                while ($filteroption->have_posts()) : $filteroption->the_post();
                  ?>

                  <div>
                    <h5><?php the_title(); ?></h5>

                  </div>
                <?php 
                endwhile; 
                else: 
                    return false; 
                endif;
            }   
            die;
            }
            ?>

Leave a Comment