Skip to content

Instantly share code, notes, and snippets.

@lagenceoueb
Last active February 23, 2022 10:51
Show Gist options
  • Save lagenceoueb/0181c62a280de2f3bc5d46a6ccecbfdf to your computer and use it in GitHub Desktop.
Save lagenceoueb/0181c62a280de2f3bc5d46a6ccecbfdf to your computer and use it in GitHub Desktop.
Add a dynamic ajax filter on top of posts' list template (WordPress). This function must be required on functions.php in your active thème and you have to create an action hook on your template to render input and result
<?php
/**
* The function to display filter args for posts.
* @author jm silone
* needs to add a hook on your view (template) in this exampl we used
* a do_action('filter_by_categories',10); hook on target template
*/
/**
* View for request input
* @return void
*/
function oueb_render_inputs() {
if( $terms = get_terms( array(
'taxonomy' => 'category', // to make it simple I use default categories
'orderby' => 'name'
) ) ) :
?>
<div class="actualites__search">
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<!-- if categories exist, display the dropdown -->
<?php foreach ($terms as $term) : ?>
<input class=""
type="radio"
id="<?= $term->term_id; ?>"
name="categoryfilter"
value="<?= $term->term_id;?>">
<label for="<?= $term->term_id ;?>"><?= $term->name;?></label>
<?php endforeach; ?>
<input type="hidden" name="action" value="myfilter">
</form>
<div id="processing">
</div>
</div>
<?php
endif;
}
/**
* Post input value and send it to php
* @return void
*/
function oueb_ajax_handle()
{
ob_start();
?>
<script>
jQuery(function($){
function handleAjaxPost(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
$('#processing').text('Recherche en cours.....'); // changing the button label
},
success:function(data){
$('#processing').text(''); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
};
handleAjaxPost();
$('#filter').change(function(){
handleAjaxPost();
});
});
</script>
<?php
echo ob_get_clean();
}
add_action('wp_ajax_myfilter', 'handle_filter_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'handle_filter_function');
/**
* let filter and display result
* @return void
*/
function handle_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'] // ASC or DESC
);
// for taxonomies / categories
if( isset( $_POST['categoryfilter'] ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array($_POST['categoryfilter']),
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
$card_posts[] = get_post($query->post->ID);
//dump(get_post($query));
endwhile;
wp_reset_postdata();
foreach ($card_posts as $card_post) {
$card_ID = $card_post->ID;
// make stuff with post ID (render post excerpt or title or whatever....
}
else :
echo __('no result for requested category','domain');
endif;
die();
}
/**
* add action to show on frontend view
*/
if(!function_exists('do_filter_by_categories')) {
function do_filter_by_categories(){
// init ajax request form
oueb_render_inputs();
// handle ajax input request
oueb_ajax_handle();
}
}
/**
* ... add them on actualities template
* on hook 'filter_by_categorie'
*/
add_action('filter_by_categories','do_filter_by_categories',10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment