Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding a Salary Filter to the Job Search Form
<?php
/**
* This can either be done with a filter (below) or the field can be added directly to the job-filters.php template file!
*
* job-manager-filter class handling was added in v1.23.6
*/
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;
}
@danny-faith

This comment has been minimized.

Copy link

danny-faith commented Aug 17, 2015

Hi, pasting this code into my functions.php file seems to have broken my Wordpress installation. I can no longer update a post/page. When I try to create/save a post page I am just greeted with a blank white page. I am using WP Job Manager 1.23.6 and Wordpress 4.2.4. Has anyone else experienced this? I've deleted tried again and then reinstalled the plugin and I still cannot create/save a post/page. I am now going to have to create a new install of Wordpress and move all my site content over amnually to avoid whatever has happened by pasting the above code.

@ZenunVee

This comment has been minimized.

Copy link

ZenunVee commented Jan 14, 2016

PHP Error. Don't copy lines 1-6. To fix this error, FTP into your site and edit the functions file from there

@gidantrip

This comment has been minimized.

Copy link

gidantrip commented Feb 2, 2018

Hello I need the same code but for RESUME not for JOB. Can you help me?
I have custom resume fiels and I want to search by those field

@mukeshtech

This comment has been minimized.

Copy link

mukeshtech commented May 15, 2018

i want add same thing for string, like i want education , how i can do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.