Create Custom Post Type and Filter by Custom Fields

functions.php


/////////////////////////////////////////////////
//
//	Custom Post Type -> Exhibition
//	Exhibition Filters
//
/////////////////////////////////////////////////

function create_exhibition_post_type() {
	$labels = array(
		'name' => esc_html__( 'Exhibitions', 'naix' ),
		'singular_name' => esc_html__( 'Exhibition', 'naix' ),
		'add_new_item' => esc_html__( 'Add New Exhibition', 'naix' ),
	);
	$args = array(
		'labels' => $labels,
		'public' => true,
		'has_archive' => true,
		'rewrite' => array('slug' => 'exhibitions'),
		'menu_icon' => 'dashicons-format-gallery',
		'supports' => array('title','editor','thumbnail','excerpt'),
		'publicly_queryable' => true,
	);
	register_post_type('exhibition', $args);
}
add_action('init', 'create_exhibition_post_type');

function exhibition_filter_function(){
	$args = array(
			'post_type' => 'exhibition',
			'posts_per_page' => 12,
	);

	if(isset($_POST['status']) && $_POST['status'] == 'all') {
		$state = '';
	}
	
	if(isset($_POST['status']) && $_POST['status'] == 'current') { //Current
		$args['meta_query'][] = array(
			array(
					'key' => 'start_date',
					'value' =>	date("Y-m-d"),
					'compare' => '<=',
					'type' => 'DATE',
			),
			array(
					'key' => 'end_date',
					'value' =>	date("Y-m-d"),
					'compare' => '>=',
					'type' => 'DATE',
			),
		);
		$state = esc_html__( 'current', 'naix' );
		
	}	

	if(isset($_POST['status']) && $_POST['status'] == 'upcomming') { // Upcomming
		$args['meta_query'][] = array(
			array(
					'key' => 'start_date',
					'value' =>	date("Y-m-d"),
					'compare' => '>',
					'type' => 'DATE',
			),
		);
		$state = esc_html__( 'upcomming', 'naix' );
	}

	if(isset($_POST['status']) && $_POST['status'] == 'past') { //Past
		$args['meta_query'][] = array(
			array(
					'key' => 'end_date',
					'value' =>	date("Y-m-d"),
					'compare' => '<',
					'type' => 'DATE',
			),
		);
		$state = esc_html__( 'past', 'naix' );
	}
		
	if(isset($_POST['place'])) { //Location Filter
		$raw_values = $_POST['place'];

		$values = array(); // Combine 3 museums in one checkbox
		foreach ($raw_values as $value) {
			if($value == 815) {
				array_push($values, 101, 104, 326);
			} else {
				$values[] = $value;
			}		
		}
		
		$meta_val = array('relation' => 'OR');
		foreach ($values as $value) {
			$meta_val[] = array(
					'key'       => 'place',
					'value'     => $value,
					'compare'   => 'LIKE',
			);
			$places[] = '<a href="'. get_the_permalink( $value ) .'">'. get_the_title( $value ) .'</a>';
		}
		$args['meta_query'][] = $meta_val;
	}

	$query = new WP_Query( $args );
	
	if( $query->have_posts() ) :
		while( $query->have_posts() ) : $query->the_post();
			get_template_part( 'template-parts/custom', 'fourcolsgrid' );
		endwhile;
		wp_reset_postdata();
	else : ?>
		<article class="col-md-12 col-sm-12 col-xs-12">
			<p><?php esc_html_e( 'There aren\'t', 'naix' ); ?> <?php esc_html_e( $state, 'naix' ); ?> <?php esc_html_e( 'exhibitions', 'naix' ); ?> <?php if ($places) esc_html_e( 'at', 'naix' ); ?> <?php echo implode(', ', $places); ?></p>
		</article>
	<?php endif;
	
	die();
}

add_action('wp_ajax_exhibitionfilter', 'exhibition_filter_function');
add_action('wp_ajax_nopriv_exhibitionfilter', 'exhibition_filter_function');

archive.php


<?php if(is_post_type_archive('exhibition')) : ?>

	<div class="filters">
		<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
			<div class="filter-row">
				<div class="filter pull-right col-md-6 col-sm-6 col-xs-12 text-right">
					<ul>
						<li><input type="radio" name="status" id="all" value="all" ><label for="all"><?php esc_html_e( 'All', 'naix' ); ?></label></li>
						<li><input type="radio" name="status" id="current" value="current" ><label for="current"><?php esc_html_e( 'Current', 'naix' ); ?></label></li>
						<li><input type="radio" name="status" id="upcomming" value="upcomming" ><label for="upcomming"><?php esc_html_e( 'Upcoming', 'naix' ); ?></label></li>
						<li><input type="radio" name="status" id="past" value="past" ><label for="past"><?php esc_html_e( 'Past', 'naix' ); ?></label></li>
					</ul>
				</div>
				<div class="switch-toggle col-md-6 col-sm-6 col-xs-12">
					<a href="#"><?php esc_html_e( 'Locations', 'naix' ); ?></a>
				</div>
				<input type="hidden" name="action" value="exhibitionfilter">
			</div>
				<?php if( $places = get_posts(array('post_type' => 'page', 'post_parent' => 7, 'posts_per_page' => -1, 'order' => 'ASC'))) :
					echo '<div class="checkbox-filter col-md-12 col-sm-12 col-xs-12"><ul>';
					echo '<li><label><input type="checkbox" name="select-all" id="select-all">'. esc_html__( 'All', 'naix' ) .'</label></li>';
						foreach ( $places as $place ) :
							echo '<li title="' . $place->post_title . '"><label><input type="checkbox" name="place[]" value="' . $place->ID . '">' . $place->post_title . '</label></li>'; // ID of the category as the value of an option
						endforeach;
					echo '</ul></div>';
				endif;
				wp_reset_postdata(); ?>	
		</form>
	</div>	

	<script>
		(function($) {
			$('#select-all').click(function(event) {   
			    if(this.checked) {
			        $(':checkbox').each(function() {
			            this.checked = true;                        
			        });
			    } else {
			    	$(":checkbox").each(function() { this.checked = false; });
				}
			})
			$('#filter').on('change',function(){
				var filter = $('#filter');
				$.ajax({
					url:filter.attr('action'),
					data:filter.serialize(),
					type:filter.attr('method'),
					beforeSend:function(xhr){
						console.log(filter.serialize());
					},
					success:function(data){
						$('.naix-post-list').html(data); // insert data
					}
				});
				return false;
			});
		})( jQuery );
	</script>

<?php endif; ?>

live url: http://nationalgallery.bg/exhibitions/