<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Custom Archives | Blog</title>
	<atom:link href="https://onclickinnovations.com/blog/tag/custom/feed/" rel="self" type="application/rss+xml" />
	<link>https://onclickinnovations.com/blog/tag/custom/</link>
	<description>Onclick Innovations Pvt. Ltd.</description>
	<lastBuildDate>Wed, 03 Aug 2016 05:27:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>
<site xmlns="com-wordpress:feed-additions:1">208843066</site>	<item>
		<title>Filter / Sort post by categories using Ajax in WordPress.</title>
		<link>https://onclickinnovations.com/blog/filter-sort-post-by-categories-using-ajax-in-wordpress/</link>
					<comments>https://onclickinnovations.com/blog/filter-sort-post-by-categories-using-ajax-in-wordpress/#comments</comments>
		
		<dc:creator><![CDATA[it_geeks]]></dc:creator>
		<pubDate>Thu, 23 Jun 2016 13:08:55 +0000</pubDate>
				<category><![CDATA[Custom Software Development Solutions]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[categories]]></category>
		<category><![CDATA[Custom]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://blog.onclickinnovations.com/?p=43</guid>

					<description><![CDATA[<p>Using Ajax in wordpress you can create your custom ajax filter to sort your post/Custom post according to categories without page refresh. Following are the procedure to achieve it : 1. HTML Note : Here filter_posts_by_category(&#8216;cat1&#8217;, 1) cat1 is slug of the category. 2. JS 3. PHP Note : Here &#8220;course&#8221; is my custom post [&#8230;]</p>
<p>The post <a href="https://onclickinnovations.com/blog/filter-sort-post-by-categories-using-ajax-in-wordpress/">Filter / Sort post by categories using Ajax in WordPress.</a> appeared first on <a href="https://onclickinnovations.com/blog">Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Using Ajax in wordpress you can create your custom ajax filter to sort your post/Custom post according to categories without page refresh.</p>
<p>Following are the procedure to achieve it :<br />
<strong>1. HTML</strong></p>
<pre class="brush: xml; title: ; notranslate">
&lt;a href=&quot;javascript:void(0);&quot;  onClick=&quot;filter_posts_by_category('cat1', 1)&quot;&gt;cat1&lt;/a&gt;
&lt;a href=&quot;javascript:void(0);&quot;  onClick=&quot;filter_posts_by_category('cat2', 1)&quot;&gt;cat2&lt;/a&gt;
&lt;a href=&quot;javascript:void(0);&quot;  onClick=&quot;filter_posts_by_category('cat3', 1)&quot;&gt;cat3&lt;/a&gt;
&lt;a href=&quot;javascript:void(0);&quot;  onClick=&quot;filter_posts_by_category('all', 1)&quot;&gt;all&lt;/a&gt;
</pre>
<p><strong>Note : Here filter_posts_by_category(&#8216;cat1&#8217;, 1) cat1 is slug of the category.</strong></p>
<p><strong>2. JS</strong></p>
<pre class="brush: jscript; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
    jQuery(document).ready(function(){
       
       
        filter_posts_by_category('all', 1);
       
    });
   
   
    var filter_posts_by_category = function(cat_slug, paged){
        var ajax_url = window.location.protocol + &quot;//&quot; + window.location.host +'/wp-admin/admin-ajax.php';
       
        var total_posts = -1; // -1 for show all posts
       
        var data = {
            'action'    : 'filter_posts_by_category',
            'cat_slug'    : cat_slug,
            'posts'        : total_posts,
            'paged'        : paged,
        };
       
        jQuery.ajax({
            method:&quot;POST&quot;,
            url: ajax_url,
            data: data,
            beforeSend : function(){
                // functionality to select unselect category
                jQuery('.filter_icon').each(function(){
                    jQuery(this).removeClass('selected_cat');
                });
               
                jQuery('.'+cat_slug).addClass('selected_cat');
               
                // function to set calender icon url to another location
                setCalenderUrl(cat_slug);
               
                jQuery('#post_filtered').html('&lt;p style=&quot;text-align:center&quot;&gt;&lt;img class=&quot;img_loader&quot; src=&quot;' + window.location.protocol + '//' + window.location.host + '/images/postloading.gif&quot; /&gt;&lt;/p&gt;');
            },
            success: function(result){
                jQuery('#post_filtered').html(result);
            },
            error: function(xhr,status,error){
                // console.log(error);
            }
        });
    }
   
    &lt;/script&gt;
</pre>
<p><strong>3. PHP</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
add_action('wp_ajax_filter_posts_by_category', 'ajax_filter_posts_by_category');
add_action('wp_ajax_nopriv_filter_posts_by_category', 'ajax_filter_posts_by_category');   
   
function ajax_filter_posts_by_category(){
    $terms = isset($_POST&#x5B;'cat_slug']) &amp;&amp; !empty($_POST&#x5B;'cat_slug']) ? $_POST&#x5B;'cat_slug'] : 'all';
    $paged = $_POST&#x5B;'paged'];
    $posts = $_POST&#x5B;'posts'];
   
    $wp_query = null;
    $wp_query = new WP_Query();
   
    if($terms != 'all'){
      $args = array(
                      'post_type' =&gt; 'cours',
                      'showposts'=&gt;$posts,
                      'paged'=&gt;$paged,
                      'tax_query' =&gt; array(
                          array(
                              'taxonomy' =&gt; 'cours_category',
                              'field'    =&gt; 'slug',
                              'terms'    =&gt; $terms
                          )
                      ),
                                          'order'=&gt;'asc',
                                          'orderby'=&gt;'title',
                  );
    }
    else{
        $args = array(
                      'post_type' =&gt; 'cours',
                      'showposts'=&gt;$posts,
                                  'paged'=&gt;$paged,
                                          'order'=&gt;'asc',
                                          'orderby'=&gt;'title',
                   
                  );
    }
   
     
    $wp_query-&gt;query($args);

    $counter = 1;
    while ($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post();
      global $post;
     // var_dump($post);
?&gt;   
     &lt;?php echo $post-&gt;post_title;  ?&gt;
&lt;?php

    endwhile;


    // Set up next and prev links
    $this_page = $wp_query-&gt;get('paged');
    $max_page = $wp_query-&gt;max_num_pages;
    if ($this_page &gt; 1) {
      echo '&lt;a class=&quot;filtered-posts-newer&quot; href=&quot;javascript:void(0);&quot; onClick=&quot;filter_posts_by_category(\''.$terms.'\','.($this_page - 1).');&quot;&gt;Newer posts&lt;/a&gt; &amp;nbsp; &amp;nbsp;';
    }
    if ($this_page &lt; $max_page) {
      echo '&lt;a class=&quot;filtered-posts-older&quot; href=&quot;javascript:void(0);&quot; onClick=&quot;filter_posts_by_category(\''.$terms.'\','.($this_page + 1).');&quot;&gt;Older posts&lt;/a&gt;';
    }
    echo &quot;&lt;/nav&gt;&quot;;
   
    die();
}
?&gt;
</pre>
<p><strong>Note : Here &#8220;course&#8221; is my custom post type and &#8220;course_category&#8221; is my custom taxonomy.</strong></p>
<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fonclickinnovations.com%2Fblog%2Ffilter-sort-post-by-categories-using-ajax-in-wordpress%2F&amp;linkname=Filter%20%2F%20Sort%20post%20by%20categories%20using%20Ajax%20in%20WordPress." title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fonclickinnovations.com%2Fblog%2Ffilter-sort-post-by-categories-using-ajax-in-wordpress%2F&amp;linkname=Filter%20%2F%20Sort%20post%20by%20categories%20using%20Ajax%20in%20WordPress." title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fonclickinnovations.com%2Fblog%2Ffilter-sort-post-by-categories-using-ajax-in-wordpress%2F&amp;linkname=Filter%20%2F%20Sort%20post%20by%20categories%20using%20Ajax%20in%20WordPress." title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_no_icon a2a_counter addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fonclickinnovations.com%2Fblog%2Ffilter-sort-post-by-categories-using-ajax-in-wordpress%2F&#038;title=Filter%20%2F%20Sort%20post%20by%20categories%20using%20Ajax%20in%20WordPress." data-a2a-url="https://onclickinnovations.com/blog/filter-sort-post-by-categories-using-ajax-in-wordpress/" data-a2a-title="Filter / Sort post by categories using Ajax in WordPress.">Share</a></p><p>The post <a href="https://onclickinnovations.com/blog/filter-sort-post-by-categories-using-ajax-in-wordpress/">Filter / Sort post by categories using Ajax in WordPress.</a> appeared first on <a href="https://onclickinnovations.com/blog">Blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://onclickinnovations.com/blog/filter-sort-post-by-categories-using-ajax-in-wordpress/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">43</post-id>	</item>
	</channel>
</rss>
