自定义过滤器更改下拉至复选框

时间:2019-02-12 16:11:32

标签: php html ajax wordpress jobs

我正在使用“ wp作业管理器”,并尝试按提交时添加到作业中的自定义字段进行过滤/搜索。插件作者提供了一个用于添加下拉菜单以过滤添加的“薪金”字段的教程,我想将该教程修改为使用复选框而不是下拉列表。任何建议将不胜感激。

<?php
add_action( 'job_manager_job_filters_search_jobs_end', 'filter_by_salary_field' );
function filter_by_salary_field() {
    ?>
    <div class="search_categories">
        <label for="search_categories"><?php _e( 'Seasons', 'wp-job-manager' ); ?></label>
        <select name="filter_by_salary" class="job-manager-filter">
            <option value="">Any Season</option>
            <option value="summer">Summer</option>
            <option value="spring">Spring</option>
            <option value="fall">Fall</option>
            <option value="winter">Winter</option>
        </select>


    </div>
    <?php
}

add_filter( 'job_manager_get_listings', 'filter_by_salary_field_query_args', 10, 2 );
function filter_by_salary_field_query_args( $query_args, $args ) {
    if ( isset( $_POST['form_data'] ) ) {
        parse_str( $_POST['form_data'], $form_data );
        // If this is set, we are filtering by salary

        if ( ! empty( $form_data['filter_by_salary'] ) ) {
            $selected_range = sanitize_text_field( $form_data['filter_by_salary'] );



            switch ( $selected_range ) {
                case 'fall' :
                    $query_args['meta_query'][] = array(
                        'key'     => '_season',
                        'value'   => 'fall',
                        'compare' => 'IN'
                    );
                    break;
                case 'spring' :
                    $query_args['meta_query'][] = array(
                        'key'     => '_season',
                        'value'   => 'spring',
                        'compare' => 'IN'
                    );
                    break;
                case 'summer' :
                    $query_args['meta_query'][] = array(
                        'key'     => '_season',
                        'value'   => 'summer',
                        'compare' => 'IN'
                    );
                    break;
                default :
                    $query_args['meta_query'][] = array(
                        'key'     => '_season',
                        'value'   => 'winter',
                        'compare' => 'IN'
                    );
                    break;
            }
            // This will show the 'reset' link
            add_filter( 'job_manager_get_listings_custom_filter', '__return_true' );
        }
    }

    return $query_args;
}

0 个答案:

没有答案