Last active
November 8, 2020 18:21
-
-
Save vladchv/d9830eb31390090f7134c6dbb092397f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ACF Filter | |
<?php $GLOBALS['my_query_filters'] = array( | |
'field_1' => 'city', | |
'field_2' => 'vac' | |
); | |
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1); | |
function my_pre_get_posts( $query ) { | |
if( is_admin() ) return; | |
if( !$query->is_main_query() ) return; | |
$meta_query[] = $query->get('meta_query'); | |
foreach( $GLOBALS['my_query_filters'] as $key => $name ) { | |
if( !empty($_GET[ $name ]) ) { | |
$value = explode(',', $_GET[ $name ]); | |
$meta_query[] = array( | |
'key' => $name, | |
'value' => $value, | |
'compare' => 'IN', | |
); | |
} | |
} | |
$query->set('meta_query', $meta_query); | |
return; | |
} ?> | |
===========================HTML:======================== | |
<div id="search-houses"> | |
<?php | |
$field = get_field_object('city'); | |
$values = explode(',', $_GET['city']); | |
$field_2 = get_field_object('vac'); | |
$values_2 = explode(',', $_GET['vac']); | |
?> | |
<div class="filterss"> | |
<div class="filter" data-filter="city"> | |
<select class="city"> | |
<option value="">Выберите регион</option> | |
<?php foreach( $field['choices'] as $choice_value => $choice_label ): ?> | |
<option value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>selected<?php endif; ?> /> <?php echo $choice_label; ?></option> | |
<?php endforeach; ?> | |
</select> | |
</div> | |
<div class="filter" data-filter="vac"> | |
<select class="vac"> | |
<option value="">Выберите категорию</option> | |
<?php foreach( $field_2['choices'] as $choice_value => $choice_label ): ?> | |
<option value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values_2) ): ?>selected<?php endif; ?> /> <?php echo $choice_label; ?></option> | |
<?php endforeach; ?> | |
</select> | |
</div> | |
</div> | |
</div> | |
<div class="columns-container vacc"> | |
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> | |
<?php get_template_part('loop-v'); ?> | |
<?php endwhile; ?> | |
<?php else: echo '<p>По данному запросу вакансий не найдено.</p><p><a class="back" href="' . get_site_url() . '/vacansii/">Вернуться к вакансиям</a></p>'; ?> | |
<?php endif; ?> | |
</div> | |
<script> | |
jQuery (function($) { | |
$('#search-houses').on('change', 'select', function(){ | |
var url = '<?php echo home_url('vacansii'); ?>'; | |
args = {}; | |
$('#search-houses .filter').each(function(){ | |
var filter = $(this).data('filter'), | |
vals = []; | |
$(this).find('option:selected').each(function(){ | |
vals.push( $(this).val() ); | |
}); | |
args[ filter ] = vals.join(','); | |
}); | |
url += '?'; | |
$.each(args, function( name, value ){ | |
url += name + '=' + value + '&'; | |
}); | |
url = url.slice(0, -1); | |
window.location.replace( url ); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment