筛选器适用于最后一个选择,但不适用于其他选定内容

时间:2021-04-05 作者:user2751645

我一直在使用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>

1 个回复
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() 例如。

相关推荐

Apply_Filters()对所需的参数进行切片

我正在尝试向WooCommerce订单中的每个退款行添加一个按钮(其功能超出了这个问题的范围,足以说明它需要退款id作为参数)。我发现这些行是在woocommerce\\includes\\admin\\meta Box\\views\\html订单退款中创建的。无法重写的php。然而,有一项行动:do_action( \'woocommerce_admin_order_item_values\', null, $refund, $refund->get_id() ); 这似乎非常适合我的