so this can be duplicate but i can't find the same question. I have a big trouble that possibly have really small cause. I want to filter my custom post type posts by their category with ajax.
What it does is that when i want to filter by my categories that are created and assigned to posts, i have a blank response with no posts. No problems from javascript console, so i assume there is so problem with names of slugs/categories.
Can you please help and look at this with 'cold head'.
My custom post type and custom taxonomy registered:
add_action( 'init', 'blog_register' );
function blog_register() {
$labels = array(
'name' => _x('Blog', 'post type general name'),
'singular_name' => _x('Blog', 'post type singular name'),
'add_new' => _x('Add New', 'work item'),
'add_new_item' => __('Add New Blog Item'),
'edit_item' => __('Edit Blog Item'),
'new_item' => __('New Blog Item'),
'view_item' => __('View Blog Item'),
'search_items' => __('Search Blog'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => '',
'taxonomies' => array ('categories')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'blog', 'with_front'=> false ),
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'blog' , $args );
register_taxonomy( 'categories', array('blog'), array(
'hierarchical' => true,
'label' => 'Categories',
'singular_label' => 'Category',
'rewrite' => array( 'slug' => 'categories', 'with_front'=> false )
)
);
register_taxonomy_for_object_type( 'categories', 'blog' ); // Better be safe than sorry
}
function filter_posts() {
$catSlug = $_POST['categories'];
$ajaxposts = new WP_Query([
'post_type' => 'blog',
'posts_per_page' => -1,
'category_name' => $catSlug,
'orderby'=> 'post_date',
'order' => 'desc',
]);
$response = '';
if($catSlug == '') {
$response .= get_template_part('template_parts/blog-item');
} else {
if($ajaxposts->have_posts()) {
while($ajaxposts->have_posts()) : $ajaxposts->the_post();
$response .= get_template_part('template_parts/blog-item');
endwhile;
} else {
echo " ";
}
}
echo $response;
exit;
}
add_action('wp_ajax_filter_posts', 'filter_posts');
add_action('wp_ajax_nopriv_filter_posts', 'filter_posts');
And here i have my js code:
jQuery(function($){
$('.cat-item').on('click', function() {
$('.cat-item').removeClass('active');
$(this).addClass('active');
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
dataType: 'html',
data: {
action: 'filter_posts',
category: $(this).data('slug'),
},
success: function(res) {
$('.blog-listing .row').html(res);
}
})
});
});
How i display my content:
<div class="blog-listing">
<div class='row'>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('template-parts/blog-item'); ?>
<?php endwhile; endif; ?>
</div>
</div>