I want to filters jobs as state and Location/city(drop). So I have addded the custom fields in job post page(state field). To that field i want to give dependency with location or city field from existing wp job manager field & filter out jobs as per state and city selection.
I am not able to fiter as per the user selection of state and city. for demo you can check here
I have tried the following code
<?php
add_filter( 'submit_job_form_fields', 'frontend_add_state_field' );
function frontend_add_state_field( $fields ) {
$fields['job']['job_state'] = array(
'label' => __( 'Project State', 'job_manager' ),
'type' => 'select',
'required' => true,
'options' => array (
'AP' => 'Andhra Pradesh',
'AR' => 'Arunachal Pradesh',
'AS' => 'Assam',
'BR' => 'Bihar',
'CT' => 'Chhattisgarh',
'GA' => 'Goa',
'GJ' => 'Gujarat',
'HR' => 'Haryana',
'HP' => 'Himachal Pradesh',
'JK' => 'Jammu & Kashmir',
'JH' => 'Jharkhand',
'KA' => 'Karnataka',
'KL' => 'Kerala',
'MP' => 'Madhya Pradesh',
'MH' => 'Maharashtra',
'MN' => 'Manipur',
'ML' => 'Meghalaya',
'MZ' => 'Mizoram',
'NL' => 'Nagaland',
'OR' => 'Odisha',
'PB' => 'Punjab',
'RJ' => 'Rajasthan',
'SK' => 'Sikkim',
'TN' => 'Tamil Nadu',
'TR' => 'Tripura',
'UK' => 'Uttarakhand',
'UP' => 'Uttar Pradesh',
'WB' => 'West Bengal',
'AN' => 'Andaman & Nicobar',
'CH' => 'Chandigarh',
'DN' => 'Dadra and Nagar Haveli',
'DD' => 'Daman & Diu',
'DL' => 'Delhi',
'LD' => 'Lakshadweep',
'PY' => 'Puducherry',
),
'priority' => 2
);
return $fields;
}
add_filter( 'job_manager_job_listing_data_fields', 'admin_add_state_field' );
function admin_add_state_field( $fields ) {
$fields['_job_state'] = array(
'label' => __( 'Project State', 'job_manager' ),
'type' => 'select',
'required' => true,
'options' => array (
'AP' => 'Andhra Pradesh',
'AR' => 'Arunachal Pradesh',
'AS' => 'Assam',
'BR' => 'Bihar',
'CT' => 'Chhattisgarh',
'GA' => 'Goa',
'GJ' => 'Gujarat',
'HR' => 'Haryana',
'HP' => 'Himachal Pradesh',
'JK' => 'Jammu & Kashmir',
'JH' => 'Jharkhand',
'KA' => 'Karnataka',
'KL' => 'Kerala',
'MP' => 'Madhya Pradesh',
'MH' => 'Maharashtra',
'MN' => 'Manipur',
'ML' => 'Meghalaya',
'MZ' => 'Mizoram',
'NL' => 'Nagaland',
'OR' => 'Odisha',
'PB' => 'Punjab',
'RJ' => 'Rajasthan',
'SK' => 'Sikkim',
'TN' => 'Tamil Nadu',
'TR' => 'Tripura',
'UK' => 'Uttarakhand',
'UP' => 'Uttar Pradesh',
'WB' => 'West Bengal',
'AN' => 'Andaman & Nicobar',
'CH' => 'Chandigarh',
'DN' => 'Dadra and Nagar Haveli',
'DD' => 'Daman & Diu',
'DL' => 'Delhi',
'LD' => 'Lakshadweep',
'PY' => 'Puducherry',
)
);
return $fields;
}
add_action( 'single_job_listing_meta_end', 'display_job_state_data' );
function display_job_state_data() {
global $post;
$state = get_post_meta( $post->ID, '_job_state', true );
if ( $state ) {
echo '<li>' . __( ' Project State: ' ) . esc_html( $state ) . '</li>';
}
}
add_action( 'job_manager_job_filters_search_jobs_end', 'filter_by_state_field' );
function filter_by_state_field() {
?>
<div class="search_categories">
<label for="search_categories"><?php _e( 'State', 'wp-job-manager' ); ?></label>
<select name="filter_by_state" class="job-manager-filter">
<option value=""><?php _e( 'All State', 'wp-job-manager' ); ?></option>
<option value="Andhra Pradesh"><?php _e( 'Andhra Pradesh', 'wp-job-manager' ); ?></option>
<option value="Arunachal Pradesh"><?php _e( 'Arunachal Pradesh', 'wp-job-manager' ); ?></option>
<option value="Assam"><?php _e( 'Assam', 'wp-job-manager' ); ?></option>
<option value="Bihar"><?php _e( 'Bihar', 'wp-job-manager' ); ?></option>
<option value="Chhattisgarh"><?php _e( 'Chhattisgarh', 'wp-job-manager' ); ?></option>
<option value="Goa"><?php _e( 'Goa', 'wp-job-manager' ); ?></option>
<option value="Gujarat"><?php _e( 'Gujarat', 'wp-job-manager' ); ?></option>
<option value="Haryana"><?php _e( 'Haryana', 'wp-job-manager' ); ?></option>
<option value="Himachal Pradesh"><?php _e( 'Himachal Pradesh', 'wp-job-manager' ); ?></option>
<option value="Jammu & Kashmir"><?php _e( 'Jammu & Kashmir', 'wp-job-manager' ); ?></option>
<option value="Jharkhand"><?php _e( 'Jharkhand', 'wp-job-manager' ); ?></option>
<option value="Karnataka"><?php _e( 'Karnataka', 'wp-job-manager' ); ?></option>
<option value="Kerala"><?php _e( 'Kerala', 'wp-job-manager' ); ?></option>
<option value="Madhya Pradesh"><?php _e( 'Madhya Pradesh', 'wp-job-manager' ); ?></option>
<option value="Maharashtra"><?php _e( 'Maharashtra', '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 '1' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Andhra Pradesh',
'compare' => 'LIKE'
);
break;
case '2' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Arunachal Pradesh',
'compare' => 'LIKE'
);
break;
case '3' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Assam',
'compare' => 'LIKE'
);
break;
case '4' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Bihar',
'compare' => 'LIKE'
);
break;
case '5' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Chhattisgarh',
'compare' => 'LIKE'
);
break;
case '6' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Goa',
'compare' => 'LIKE'
);
break;
case '7' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Gujarat',
'compare' => 'LIKE'
);
break;
case '8' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Haryana',
'compare' => 'LIKE'
);
break;
case '9' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Himachal Pradesh',
'compare' => 'LIKE'
);
break;
case '10' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Jammu & Kashmir',
'compare' => 'LIKE'
);
break;
case '11' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Jharkhand',
'compare' => 'LIKE'
);
break;
case '12' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Karnataka',
'compare' => 'LIKE'
);
break;
case '13' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Maharashtra',
'compare' => 'LIKE'
);
break;
case '14' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Kerala',
'compare' => 'LIKE'
);
break;
case '15' :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => 'Madhya Pradesh',
'compare' => 'LIKE'
);
break;
default :
$query_args['meta_query'][] = array(
'key' => '_job_state',
'value' => array_map( 'absint', explode( '-', $selected_range ) ),
'compare' => 'LIKE'
);
break;
}
// This will show the 'reset' link
add_filter( 'job_manager_get_listings_custom_filter', '__return_true' );
}
}
return $query_args;
}
?>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire