我一直在使用ACF自定义字段开发前端过滤器。我可以按最后的选择状态进行筛选。但无法选择任何其他选项。下面是我的代码,如有任何帮助,将不胜感激。
我想问题就在这里。如果你需要更多信息,请告诉我。提前谢谢。
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 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>
SO网友:Andrew
据我所知,您希望人们能够添加多个过滤器,但上面的代码一次只允许使用一个过滤器。如果此假设成立,则需要将代码更新为添加到meta_query
数组,而不是在每个条件块中替换它。例如
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 );
}
除此之外,你应该始终
sanitize user input 然后
validate the data 在使用它之前,您需要。我用过
sanitize_text_field()
例如。