Created
February 11, 2020 17:37
-
-
Save Garconis/5fdfc108fed699e81fb4ed18e373e47e to your computer and use it in GitHub Desktop.
WP Job Manager | Add custom dropdown field to admin, frontend listing, list of jobs, and search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'job_manager_job_listing_data_fields', 'admin_add_hours_field' ); | |
function admin_add_hours_field( $fields ) { | |
$fields['_job_hours'] = array( | |
'label' => __( 'Job Hours', 'job_manager' ), | |
'type' => 'select', | |
'options' => array('Select hours', '0 - 5' => '0 - 5', '5 - 10' => '5 - 10', '10 - 15' => '10 - 15', '20 +' => '20 +', 'flexible hours' => 'flexible hours'), | |
'placeholder' => '', | |
'description' => '' | |
); | |
return $fields; | |
} | |
/* Output the Hours value on the frontend of the actual Job Listing post */ | |
add_action( 'single_job_listing_meta_end', 'display_job_hours_data' ); | |
function display_job_hours_data() { | |
global $post; | |
$hours = get_post_meta( $post->ID, '_job_hours', true ); | |
if ( $hours ) { | |
echo '<li class="fs-job-hours">' . __( 'Hours:' ) . ' <span>' . esc_html( $hours ) . '</span></li>'; | |
} | |
} | |
/* Output the Hours value in the list of Jobs */ | |
add_action( 'job_listing_meta_end', 'display_job_hours_in_list' ); | |
function display_job_hours_in_list() { | |
global $post; | |
$hours = get_post_meta( $post->ID, '_job_hours', true ); | |
if ( $hours ) { | |
echo '<li class="fs-job-hours-list">' . __( 'Hours:' ) . ' <span>' . esc_html( $hours ) . '</span></li>'; | |
} | |
} | |
/** | |
* Add new option to the Search form | |
* | |
* 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_hours_field' ); | |
function filter_by_hours_field() { | |
?> | |
<div class="search_categories"> | |
<label for="search_categories"><?php _e( 'Hours', 'wp-job-manager' ); ?></label> | |
<select name="filter_by_hours" class="job-manager-filter"> | |
<option value=""><?php _e( 'How many hours per week do you want to work?', 'wp-job-manager' ); ?></option> | |
<option value="0-5"><?php _e( '0 - 5', 'wp-job-manager' ); ?></option> | |
<option value="5-10"><?php _e( '5 - 10', 'wp-job-manager' ); ?></option> | |
<option value="10-15"><?php _e( '10 - 15', 'wp-job-manager' ); ?></option> | |
<option value="15-20"><?php _e( '15 - 20', 'wp-job-manager' ); ?></option> | |
<option value="20+"><?php _e( '20 +', 'wp-job-manager' ); ?></option> | |
<option value="flexible-hours"><?php _e( 'flexible hours', 'wp-job-manager' ); ?></option> | |
</select> | |
</div> | |
<?php | |
} | |
/** | |
* This code gets your posted field and modifies the actual job search query based on the field added via the above function | |
*/ | |
add_filter( 'job_manager_get_listings', 'filter_by_hours_field_query_args', 10, 2 ); | |
function filter_by_hours_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 hours | |
if ( ! empty( $form_data['filter_by_hours'] ) ) { | |
$selected_range = sanitize_text_field( $form_data['filter_by_hours'] ); | |
switch ( $selected_range ) { | |
case '0-5' : | |
$query_args['meta_query'][] = array( | |
'key' => '_job_hours', | |
'value' => '0 - 5', | |
'compare' => '=', | |
'type' => 'CHAR' | |
); | |
break; | |
case '5-10' : | |
$query_args['meta_query'][] = array( | |
'key' => '_job_hours', | |
'value' => '5 - 10', | |
'compare' => '=', | |
'type' => 'CHAR' | |
); | |
break; | |
case '10-15' : | |
$query_args['meta_query'][] = array( | |
'key' => '_job_hours', | |
'value' => '10 - 15', | |
'compare' => '=', | |
'type' => 'CHAR' | |
); | |
break; | |
case '15-20' : | |
$query_args['meta_query'][] = array( | |
'key' => '_job_hours', | |
'value' => '15 - 20', | |
'compare' => '=', | |
'type' => 'CHAR' | |
); | |
break; | |
case '20+' : | |
$query_args['meta_query'][] = array( | |
'key' => '_job_hours', | |
'value' => '20 +', | |
'compare' => '=', | |
'type' => 'CHAR' | |
); | |
break; | |
case 'flexible-hours' : | |
$query_args['meta_query'][] = array( | |
'key' => '_job_hours', | |
'value' => 'flexible hours', | |
'compare' => '=', | |
'type' => 'CHAR' | |
); | |
break; | |
default : | |
$query_args['meta_query'][] = array( | |
); | |
break; | |
} | |
// This will show the 'reset' link | |
add_filter( 'job_manager_get_listings_custom_filter', '__return_true' ); | |
} | |
} | |
return $query_args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent, though it does not add the field to the front facing post a job form.