I'm trying to code an ajax custom post filter but the result seems like it's not filtering, and I'm not sure where I did wrong. I'll provide the revelant code below.
archive-projetos.php
<div class="categories">
<ul>
<li><a href="" class="js-filter-item">Todos</a></li>
<?php
$cat_args = array(
'exclude' => array(1),
'option_all' => 'All',
'taxonomy' => 'Tipos de Construção'
);
$categories = get_categories($cat_args);
foreach($categories as $cat): ?>
<li><a class="js-filter-item" href="<?= get_category_link($cat->term_id);?>"><?= $cat->name;?></a></li>
<?php endforeach;?>
</ul>
</div>
<div class="content-wrapper js-filter row">
<?php
$args = array (
'post_type' => array('projetos'),
'post_status' => array('publish')
);
$query = new WP_Query($args);
if($query->have_posts()):
while ($query->have_posts()) : $query->the_post();?>
<div class="post-thumbnail-container col-sm">
<a href="<?php the_permalink();?>">
<div class="thumbnail-container"><?php the_post_thumbnail();?></div>
<div class="title-overlay"><?php the_title();?></div>
</a>
</div>
<?php endwhile; else: endif; wp_reset_postdata();?>
</div>
filter.js
(function($){
$(document).ready(function(){
$(document).on('click', '.js-filter-item', function(e){
e.preventDefault();
var category =$(this).data('category');
$.ajax({
url: wpajax.ajaxurl,
data: {action: 'filter', category: category},
type: 'post',
success: function(result) {
$('.js-filter').html(result);
},
error: function(result) {
console.warn(result);
}
});
});
});
})(jQuery);
functions.php
function load_scripts() {
wp_enqueue_script('ajax', get_template_directory_uri() . '/js/filter.js', array ('jquery'), NULL, true);
wp_localize_script('ajax','wpajax',
array('ajaxurl' => admin_url('admin-ajax.php'))
);
}
add_action('wp_enqueue_scripts', 'load_scripts');
add_action('wp_ajax_nopriv_filter', 'filter_ajax');
add_action('wp_ajax_filter', 'filter_ajax');
function filter_ajax() {
$category = $_POST['projetos'];
$args = array (
'post_type' => array('projetos'),
'post_status' => array('publish')
);
$query = new WP_Query($args);
if(isset($category)) {
$args['category__in'] = array($category);
}
if($query->have_posts()):
while ($query->have_posts()) : $query->the_post();?>
<div class="post-thumbnail-container col-sm">
<a href="<?php the_permalink();?>">
<div class="thumbnail-container"><?php the_post_thumbnail();?></div>
<div class="title-overlay"><?php the_title();?></div>
</a>
</div>
<?php endwhile; else: endif; wp_reset_postdata();
die();
}
I don't think it would be necessary to include the custom post and category but there it goes as well:
function projetos()
{
$args = array(
'labels' => array(
'name'=> 'Projetos',
'singular_name'=>'Projeto'
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-category',
'supports' => array('title','editor','thumbnail')
);
register_post_type('projetos', $args);
}
add_action('init', 'projetos');
function tipos_construcao()
{
$args = array(
'labels' => array (
'name' => 'Tipos de Construção',
'singular_name'=>'Tipo de Construção'
),
'public'=> true,
'hierarchical'=> true
);
register_taxonomy('Tipos de Construção',array('projetos'), $args);
}
add_action('init', 'tipos_construcao');
Any help would be appreciated! I've been trying several solutions and tried to browse other websites but to no solution.
I did console.log(result) in the filter.js to check what was the result, and it just doesn't filter, the result is all the posts.
Thank you, once again!
<a>
in theforeach
loop inarchive-projetos.php
which has the CSS classjs-filter-item
needs to have adata-category
attribute. E.g. Adddata-category="<?php echo $cat->term_id; ?>"
to that<a>
tag.data-category
, in yourfilter_ajax()
function, switch the position of the$query = new WP_Query($args)
with theif(isset($category)) { ... }
.$category = $_POST['projetos'];
as maybe I thought'projetos'
was the culprit behind displaying all the posts, so I tried to change it to'category'
but the output is empty. while if I use'projetos'
the output is all the posts, also tried to erase theif(isset($category))
and place thecategory__in
directly in the array above, but the output looks to be the same.