I've been following this tutorial: http://pixelers.net/filter-wordpress-posts-by-category-with-ajax/. and working.
I want to create a filter based on the popular and recent post. For popular post based on a lot of the post being viewed.
For "Recent" bring up the last post. For "Popular" only display post based on the number of most viewed.
index.php
<div class="col-sm-12">
<ul class="post-filters">
<li><a href="">Recent</a></li>
<li><a href="">Popular</a></li>
</ul>
</div>
<main id="main" class="content site-main">
<?php $query_args = array(
'post_type' => 'post',
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
);
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-md-3">
<?php get_template_part( 'template-parts/content', 'grid' ); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main>
ajax.php
jQuery( document ).ready( function ( $ ) {
var $mainContent = $( '#main' ),
$cat_links = $( '.post-filters li a' );
$cat_links.on( 'click', function ( e ) {
e.preventDefault();
$el = $(this);
var value = $el.attr( "href" );
$mainContent.animate({opacity: "0.7"});
$mainContent.load(value + " #main", function(){
$mainContent.animate({opacity: "1"});
});
});
});
How can I make a link recent and popular it can Clicked and filter.
Thank you.