Job Manager Custom Filter

I am using "wp job manager" and am trying to filter / search by a custom field that I added to jobs when submitting. Plugin author offers a tutorial for adding a dropdown menu to filter the added "salary" field, and I would like to modify this tutorial to use a textbox instead of a dropdown.

The tutorial is here: https://wpjobmanager.com/document/tutorial-adding-a-salary-field-for-jobs/

Assuming the data is correctly submitted with the job (in my case it is), the following code allows you to filter jobs with a drop down menu by choosing between price ranges.

    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( 'Salary', 'wp-job-manager' ); ?></label>
        <select name="filter_by_salary" class="job-manager-filter">
            <option value=""><?php _e( 'Any Salary', 'wp-job-manager' ); ?></option>
            <option value="upto20"><?php _e( 'Up to $20,000', 'wp-job-manager' ); ?></option>
            <option value="20000-40000"><?php _e( '$20,000 to $40,000', 'wp-job-manager' ); ?></option>
            <option value="40000-60000"><?php _e( '$40,000 to $60,000', 'wp-job-manager' ); ?></option>
            <option value="over60"><?php _e( '$60,000+', '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_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 'upto20' :
                    $query_args['meta_query'][] = array(
                        'key'     => '_job_salary',
                        'value'   => '20000',
                        'compare' => '<',
                        'type'    => 'NUMERIC'
                    );
                break;
                case 'over60' :
                    $query_args['meta_query'][] = array(
                        'key'     => '_job_salary',
                        'value'   => '60000',
                        'compare' => '>=',
                        'type'    => 'NUMERIC'
                    );
                break;
                default :
                    $query_args['meta_query'][] = array(
                        'key'     => '_job_salary',
                        'value'   => array_map( 'absint', explode( '-', $selected_range ) ),
                        'compare' => 'BETWEEN',
                        'type'    => 'NUMERIC'
                    );
                break;
            }
            // This will show the 'reset' link
            add_filter( 'job_manager_get_listings_custom_filter', '__return_true' );
        }
    }
    return $query_args;
}

      

What I need to do is change the dropdown menu to a text field and if I search for "20,000", in order to only show jobs with a saved "salary" of 20,000. (In the end it won't be a salary field, but the tutorial seemed good base to start)

I have made several attempts but have not been able to make this work properly. By changing the input to a textbox and removing the options, I have limited search capability, eg. I can find "over60" and jobs with a value of 60000 or more will be shown, but searching for 60000 (or any random selection of letters and numbers) brings up all jobs without filtering.

+3


source to share


1 answer


Assuming you've already added the salary field to the WP Job Manager documentation, this code should give you what you're looking for:

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( 'Search Salary Amounts', 'wp-job-manager' ); ?></label>
        <input type="text" class="job-manager-filter" name="filter_by_salary" placeholder="Search Salary Amounts">
    </div>
    <?php
}
/**
 * This code gets your posted field and modifies the job search query
 */
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'] ) ) {
            $salary = sanitize_text_field( $form_data['filter_by_salary'] );
            $query_args['meta_query'][] = array(
                'key'     => '_job_salary',
                'value'   =>  $salary,
                'compare' => '=',
                'type'    => 'NUMERIC'
            );

            // This will show the 'reset' link
            add_filter( 'job_manager_get_listings_custom_filter', '__return_true' );
        }
    }
    return $query_args;
}

      

It will do as you requested and return results that match exactly as they were entered when the job was submitted. If you are submitting a job with a number in the "55,000" salary field, you will need a comma to find the job list on the job search page. if the user searches for the salary "55000", in this case the results will not be returned, however the result will be returned if they search for "55,000"

If you want to account for various currency symbols (like dollar signs, commas and trailing cents), you may need to look into regular expressions: http://php.net/manual/en/function.preg-replace.php



You mentioned that you will want to use it for something other than a salary, so additional modification will need to be made if you want to use it to find content other than a number. Among other possible changes, one line that you probably want to remove is:

'type'    => 'NUMERIC'

      

Note. ... When passing these items through the "job_manger_get_listings" filter, you are sending this data to a function using WP_Query. When you return $ code_args ['meta_query'] in your code, you are more specifically using the WP_Meta_Query part of WP_Query. Knowing that it might help change the code above. See additional documentation: https://codex.wordpress.org/Class_Reference/WP_Meta_Query

+4


source







All Articles