How To Add Ajax Pagination On Ajax Post Filter Result
I am developing a website with taxonomy filter. I have created taxonomy widget like this Tax A Tax B Tax C Tax D And place one apply filter below that when i clicking apply filte
Solution 1:
I think you need to use json_encode
to get the current $query_var
for the selected filters!
Added ob_start();
and ob_get_contents();
to pass posts to variable
Change you filter function with this and tell me if it work.
I hope it help!
//functon for ajax blog filter
add_action('wp_ajax_myfilter', 'wph_filter_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'wph_filter_function');
function wph_filter_function(){
// $CurrentPage = get_query_var('paged');
//$paged = $_POST['paged'];
//wp_reset_postdata();
if( isset( $_POST['categoryfilter'] ) ){
$category_post = $_POST['categoryfilter'];
}
if( isset( $_POST['categoryfilter_prod'] ) ){
$category_product = $_POST['categoryfilter_prod'];
}
if( isset( $_POST['categoryfilter_resource'] ) ){
$category_resource = $_POST['categoryfilter_resource'];
}
//var_dump($category_post);
//var_dump($category_product);
//var_dump($category_resource);
$tax_query = array('relation' => 'AND');
if (isset($_POST['categoryfilter']))
{
$tax_query[] = array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_post
);
}
if (isset($_POST['categoryfilter_prod']))
{
$tax_query[] = array(
'taxonomy' => 'cat_product',
'field' => 'id',
'terms' => $category_product
);
}
if (isset($_POST['categoryfilter_resource']))
{
$tax_query[] = array(
'taxonomy' => 'resource',
'field' => 'id',
'terms' => $category_resource
);
}
$query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' =>30,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $CurrentPage,
'tax_query' => $tax_query,
)
);
if( $query->have_posts() ) :
ob_start();
while( $query->have_posts() ): $query->the_post();
?>
<?phpif( get_field('show_login_only') ): ?><divclass="res-entry"><divclass="row"><divclass="col-md-4"><?phpif ( has_post_thumbnail()) the_post_thumbnail('resource-blog');
?></div><divclass="col-md-8 blog-details"><aclass="related-title"href="<?php the_permalink(); ?>"title="<?phpecho esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a><br><divclass="posted-date"><?php palliance_posted_on_noslash(); ?></div><divclass="post-less"><?phpecho excerpt(20); ?></div></div></div></div><?phpendif; ?><?phpendwhile;
wp_reset_postdata();
if ( $query->max_num_pages > 1 ) :
echo'<div id="misha_loadmore">More posts</div>'; // you can use <a> as wellendif;
$content = ob_get_contents(); // we pass the posts to variable
ob_end_clean(); // clear the bufferelse :
ob_start(); // start the buffer to capture the output of the template
get_template_part( 'template-parts/content', 'none' );
$content = ob_get_contents(); // pass the output to variable
ob_end_clean();
endif;
echo json_encode( array(
'posts' => json_encode( $wp_query->query_vars ),
'max_page' => $wp_query->max_num_pages,
'found_posts' => $wp_query->found_posts,
'content' => $content
));
die();
}
Post a Comment for "How To Add Ajax Pagination On Ajax Post Filter Result"