Freelance of Dev biznet
11/13/2019 - 5:10 PM

Filter main query by taxonomy

Permet d'appliquer des filtres à la query de base de WordPress en fonction des taxonomies sur les pages index.php et archive.php.

<?php get_header(); ?>

	<div id="primary" class="content-area">
		
    <main id="main" class="site-main" role="main">
			
			<?php if ( have_posts() ) : ?>

				<header class="page-header">
					<?php
						the_archive_title( '<h1 class="page-title">', '</h1>' );					
						the_archive_description( '<div class="taxonomy-description">', '</div>' );
					?>
				</header>

				<?php $filters = json_decode(stripslashes($_COOKIE['filter']), true); ?>

				<form class="filtre">
          
          <!-- Filtre sur taxonomie 'Machin' -->
					<?php 
					  $machins = get_terms( array(
					  	'taxonomy' => 'machin',
				  	)); 
				  ?>
						
					<?php foreach($machins as $machin) : ?>
						<label>
						  </label><input type="checkbox" name="machin[]" value="<?= $machin->slug ?>" <?= $checked = (in_array($machin->slug, $filters['machin'])) ? 'checked' : '' ?>>
						  <?= $machin->name ?>
						</label>
					<?php endforeach; ?>
					
					<!-- Filtre sur taxonomie 'Truc' -->
					<?php 
					  $trucs = get_terms( array(
					  	'taxonomy' => 'truc',
				  	)); 
				  ?>
						
					<?php foreach($trucs as $truc) : ?>
						<label>
						  </label><input type="checkbox" name="truc[]" value="<?= $truc->slug ?>" <?= $checked = (in_array($truc->slug, $filters['truc'])) ? 'checked' : '' ?>>
						  <?= $truc->name ?>
						</label>
					<?php endforeach; ?>
					
					<input type="submit" value="Filtrer"/>
				  <a href="#" id="reInit">Réinitialiser</a>

				</form>

				<?php while ( have_posts() ) : the_post(); ?>					
					<?php get_template_part( 'content' ); ?>
				<?php endwhile; ?>

				<?php echo paginate_links() ?>

			<?php else : ?>

				<?php get_template_part( 'content', 'none' ); ?>

			<?php endif; ?>

		</main>
		
	</div>
	
<?php get_footer(); ?>
jQuery(document).ready(function($) {
  
	$('.filtre').submit(function(e) {
    e.preventDefault();

    $('.filtre input').prop('disabled', true);

    var form = $(this);
    var data = new FormData(form[0]);

    var request = $.ajax({
			method: 'POST',
			url: '/wp-content/themes/biznet-template/ajax/ajax-filter.php',
      data: data,
      processData: false,
			contentType: false
    })

    request.success(function(data) {
      location.reload();
		});
	});
	
	$('#reInit').click(function(e) {
    e.preventDefault();

    $('.filtre input').prop('disabled', true);

    var request = $.ajax({
			method: 'POST',
			url: '/wp-content/themes/biznet-template/ajax/ajax-filter.php'
    })

    request.success(function(data) {
      location.reload();
		});
  });
  
});
<?php
 
	$path = $_SERVER['DOCUMENT_ROOT'];
 
	include_once $path . '/wp-config.php';
	include_once $path . '/wp-load.php';
	include_once $path . '/wp-includes/wp-db.php';
	include_once $path . '/wp-includes/pluggable.php';

	$filters = array(
		'machin' => (is_array($_POST['machin']) ? $_POST['machin'] : array()),
		'truc' => (is_array($_POST['truc']) ? $_POST['truc'] : array())
	);

	setcookie('filter', json_encode($filters), time()+60*60*24*30, '/', 'locandroll.com', true); 

?>
<?php
  function filter_query( $query ) {
  	if( $query->is_main_query() && ($query->is_home() || $query->is_archive()) ) {
  		$filters = json_decode(stripslashes($_COOKIE['filter']), true);
  
  		$taxquery = array();
  		$queries = 0;
  
  		if(!empty($filters['machin'])) {
  			$taxquery[] = array(
  				'taxonomy' => 'machin',
  				'field' => 'slug',
  				'terms' => $filters['machin']
  			);
  
  			$queries++;
  		}
  
  		if(!empty($filters['truc'])) {
  			$taxquery[] = array(
  				'taxonomy' => 'truc',
  				'field' => 'slug',
  				'terms' => $filters['truc']
  			);
  
  			$queries++;
  		}
  
  		if($queries > 1) {
  			$taxquery['relation'] = 'AND';
  		}
  
  		$query->set( 'tax_query', $taxquery );
  	}
  }
  add_action( 'pre_get_posts', 'filter_query' );
?>