首页 > 解决方案 > 如何为 wp Job manager 制作自定义过滤器

问题描述

嗨,我希望有人可以帮助我,我正在使用 wp 作业管理器插件,并且我正在寻找如何根据我在属性字段(即游泳池、海滩访问)中创建的字段实现我自己的过滤器,因此任何添加了游泳池、海滩的属性访问将显示,如果可能的话,我希望能够多选这个代码是从添加薪水字段向正确方向迈出的一步,这可以修改为我正在寻找的规格,我真的很难找到解决方案,基本上它是为了属性站点能够在下拉列表中显示我自己的自定义字段

add_action( 'job_manager_job_filters_search_jobs_end', 'filter_by_disabled_access_field' );

function filter_by_disabled_access_field() {
    ?>
    <div class="search_categories">
        <label for="search_categories"><?php _e( 'disabled access', 'wp-job-manager' ); ?></label>
        <select name="filter_by_disabled_access" class="job-manager-filter">
            <option value=""><?php _e( 'Disabled Access', 'wp-job-manager' ); ?></option>
            <option value="yes"><?php _e( 'Yes', 'wp-job-manager' ); ?></option>
            <option value="no"><?php _e( 'No', 'wp-job-manager' ); ?></option>
        </select>
    </div>
    <?php
}

/**
 * This code gets your posted field and modifies the job search query
 */
add_filter( 'job_manager_get_listings', 'filter_by_disabled_access_field_query_args', 10, 2 );

function filter_by_disabled_access_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_disabled_access'] ) ) {
            $selected_range = sanitize_text_field( $form_data['filter_by_disabled_access'] );
            switch ( $selected_range ) {
                case 'yes' :
                    $query_args['meta_query'][] = array(
                        'key'     => '_disabled_access',
                        'value'   => 'yes',
                        'compare' => '>=',
                        'type'    => 'TEXT'
                    );
                break;
                case 'no' :
                    $query_args['meta_query'][] = array(
                        'key'     => '_disabled_access',
                        'value'   => 'no',
                        'compare' => '=<',
                        'type'    => 'TEXT'
                    );
                break;
                default :
                    $query_args['meta_query'][] = array(
                        'key'     => '_disabled_access',
                        'value'   => array_map( 'absint', explode( '-', $selected_range ) ),
                        'compare' => 'BETWEEN',
                        'type'    => 'TEXT'
                    );
                break;
            }

        
            add_filter( 'job_manager_get_listings_custom_filter', '__return_true' );
        }
    }
    return $query_args;
}

到目前为止我有这个工作但是只有一个选项禁用访问我想添加更多来过滤掉这怎么可能

标签: phpwordpresscustom-post-type

解决方案


推荐阅读