admin管理员组文章数量:1296248
I have been working on a frontend filter using ACF Custom Fields. I can filter by the last selection states. but can't select any other option. Below is my code any help would be greatly appreciated.
I am thinking the problem is in here. If you need more info please let me know. Thanks in advance.
add_action('wp_ajax_myfilter', 'ransom_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'ransom_filter_function');
function ransom_filter_function(){
$s = $_POST['s'];
$iperson = $_POST['iperson'];
$country = $_POST['country'];
$state = $_POST['state'];
$args = array (
'post_type' => 'myposttype',
'posts_per_page' => -1,
's' => $s,
);
// Inperson Virtual Checkbox
if( isset( $iperson ) && !empty( $iperson ) )
$args['meta_query'] = array(
array (
'key' => 'iperson_virtual',
'value' => '"'.$iperson.'"',
'compare' => 'LIKE'
),
);
// Country Dropdown
if( isset( $country ) && $country )
$args['meta_query'] = array(
array (
'value' => $country,
'compare' => '='
),
);
// State Dropdown
if( isset( $state ) && $state )
$args['meta_query'] = array(
array (
'value' => $_POST['state'],
'compare' => '='
),
);
$query = new WP_Query( $args );
Form page call
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<label>
<input type="text" name='s'>
</label>
<!-- Inperson Checkbox -->
<?php
if( have_rows('basic_info') ){
// Loop through rows.
while( have_rows('basic_info') ) { the_row();
$ipvs = get_sub_field( 'inperson' );
$field = get_field_object('field_60457a3f8f347');
$choices = $field['choices'];
?>
<ul>
<?php foreach( $choices as $choice => $label ) : ?>
<input type="checkbox" name="iperson" id="ipvfilter" value="<?php echo $choice; ?>" /><?php echo $label; ?>
<?php endforeach; ?>
</ul>
<?php } ?>
<?php } ?>
<!-- Country Select -->
<?php
if( have_rows('basic_info') ){
// Loop through rows.
while( have_rows('basic_info') ) { the_row();
$race_country = get_sub_field( 'country' );
$country_field = get_field_object('field_60457b018f348');
$countries = $country_field['choices'];
?>
<select id="country" name="country" class="country">
<option>Select Country</option>
<?php foreach( $countries as $country => $label ) : ?>
<option name="country" id="country" value="<?php echo $country; ?>"><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
<?php } ?>
<?php } ?>
<!-- State Select -->
<?php if( have_rows('basic_info') ){
// Loop through rows.
while( have_rows('basic_info') ) { the_row();
$race_state = get_sub_field( 'state' );
$state_field = get_field_object('field_60457b4d8f349');
$states = $state_field['choices'];
?>
<select id="state" name="state" class="state">
<option>Select State</option>
<?php foreach( $states as $state => $label ) : ?>
<option name="state" id="state" value="<?php echo $state; ?>" ><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
<?php } ?>
<?php } ?>
<button>Apply filter</button>
<input type="hidden" name="action" value="myfilter">
</form>
I have been working on a frontend filter using ACF Custom Fields. I can filter by the last selection states. but can't select any other option. Below is my code any help would be greatly appreciated.
I am thinking the problem is in here. If you need more info please let me know. Thanks in advance.
add_action('wp_ajax_myfilter', 'ransom_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'ransom_filter_function');
function ransom_filter_function(){
$s = $_POST['s'];
$iperson = $_POST['iperson'];
$country = $_POST['country'];
$state = $_POST['state'];
$args = array (
'post_type' => 'myposttype',
'posts_per_page' => -1,
's' => $s,
);
// Inperson Virtual Checkbox
if( isset( $iperson ) && !empty( $iperson ) )
$args['meta_query'] = array(
array (
'key' => 'iperson_virtual',
'value' => '"'.$iperson.'"',
'compare' => 'LIKE'
),
);
// Country Dropdown
if( isset( $country ) && $country )
$args['meta_query'] = array(
array (
'value' => $country,
'compare' => '='
),
);
// State Dropdown
if( isset( $state ) && $state )
$args['meta_query'] = array(
array (
'value' => $_POST['state'],
'compare' => '='
),
);
$query = new WP_Query( $args );
Form page call
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<label>
<input type="text" name='s'>
</label>
<!-- Inperson Checkbox -->
<?php
if( have_rows('basic_info') ){
// Loop through rows.
while( have_rows('basic_info') ) { the_row();
$ipvs = get_sub_field( 'inperson' );
$field = get_field_object('field_60457a3f8f347');
$choices = $field['choices'];
?>
<ul>
<?php foreach( $choices as $choice => $label ) : ?>
<input type="checkbox" name="iperson" id="ipvfilter" value="<?php echo $choice; ?>" /><?php echo $label; ?>
<?php endforeach; ?>
</ul>
<?php } ?>
<?php } ?>
<!-- Country Select -->
<?php
if( have_rows('basic_info') ){
// Loop through rows.
while( have_rows('basic_info') ) { the_row();
$race_country = get_sub_field( 'country' );
$country_field = get_field_object('field_60457b018f348');
$countries = $country_field['choices'];
?>
<select id="country" name="country" class="country">
<option>Select Country</option>
<?php foreach( $countries as $country => $label ) : ?>
<option name="country" id="country" value="<?php echo $country; ?>"><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
<?php } ?>
<?php } ?>
<!-- State Select -->
<?php if( have_rows('basic_info') ){
// Loop through rows.
while( have_rows('basic_info') ) { the_row();
$race_state = get_sub_field( 'state' );
$state_field = get_field_object('field_60457b4d8f349');
$states = $state_field['choices'];
?>
<select id="state" name="state" class="state">
<option>Select State</option>
<?php foreach( $states as $state => $label ) : ?>
<option name="state" id="state" value="<?php echo $state; ?>" ><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
<?php } ?>
<?php } ?>
<button>Apply filter</button>
<input type="hidden" name="action" value="myfilter">
</form>
Share
Improve this question
edited Apr 6, 2021 at 0:29
user2751645
asked Apr 5, 2021 at 1:32
user2751645user2751645
12 bronze badges
1 Answer
Reset to default 0From what I understand, you want people to be able to add multiple filters but the code above is only allowing one filter to be used at a time. If this assumption is true, you'll need to update your code to add to the meta_query
array rather than replace it in each conditional block. For example.
function ransom_filter_function() {
$s = sanitize_text_field( $_POST['s'] );
$iperson = sanitize_text_field( $_POST['iperson'] );
$country = sanitize_text_field( $_POST['country'] );
$state = sanitize_text_field( $_POST['state'] );
$args = array(
'post_type' => 'myposttype',
'posts_per_page' => - 1,
's' => $s,
);
$meta_query = array();
// Inperson Virtual Checkbox
if ( isset( $iperson ) && ! empty( $iperson ) ) {
$meta_query[] = array(
'key' => 'iperson_virtual',
'value' => '"' . $iperson . '"',
'compare' => 'LIKE'
);
}
// Country Dropdown
if ( isset( $country ) && $country ) {
$meta_query[] = array(
'value' => $country,
'compare' => '='
);
}
// State Dropdown
if ( isset( $state ) && $state ) {
$meta_query[] = array(
'value' => $state,
'compare' => '='
);
}
if ( ! empty( $meta_query ) ) {
$args['meta_query'] = $meta_query;
}
$query = new WP_Query( $args );
}
An aside to the above, you should always sanitize user input and then validate the data you have is expected before using it. I've used sanitize_text_field()
as an example.
本文标签: functionsFilter works on last selection but no others
版权声明:本文标题:functions - Filter works on last selection but no others 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741632952a2389472.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论