How to set max selections for different term-multiselect fields (required template override)

term-multiselect-field.php

<?php
// You need to use this file as a template override for this to work,
// for more details please see the wpjobmanager.com documentation:
// https://wpjobmanager.com/document/template-overrides/

// Get selected value
if ( isset( $field['value'] ) ) {
	$selected = $field['value'];
} elseif (  ! empty( $field['default'] ) && is_int( $field['default'] ) ) {
	$selected = $field['default'];
} elseif ( ! empty( $field['default'] ) && ( $term = get_term_by( 'slug', $field['default'], $field['taxonomy'] ) ) ) {
	$selected = $term->term_id;
} else {
	$selected = '';
}

wp_enqueue_script( 'wp-job-manager-term-multiselect' );
// Below is our custom added jQuery to set options for specific fields (ID will always be the meta key)
?>

<script>
jQuery(function($){
	$( '#job_tags' ).chosen({
	  max_selected_options: 10,
	  search_contains: true
	});
	$( '#job_category' ).chosen({
	  max_selected_options: 5,
	  search_contains: true
	});	
});
</script>

<?php

$args = array(
	'taxonomy'     => $field['taxonomy'],
	'hierarchical' => 1,
	'name'         => isset( $field['name'] ) ? $field['name'] : $key,
	'orderby'      => 'name',
	'selected'     => $selected,
	'hide_empty'   => false
);

// Here we check for the fields from above, and remove the 'job-manager-category-dropdown' class
// to prevent that field from being set using the default chosen arguments
$skip_chosen_classes = array('job_tags', 'job_category');
if( in_array( $args['name'], $skip_chosen_classes ) ) $args['class'] = is_rtl() ? 'chosen-rtl' : '';

if ( isset( $field['placeholder'] ) && ! empty( $field['placeholder'] ) ) $args['placeholder'] = $field['placeholder'];

job_manager_dropdown_categories( apply_filters( 'job_manager_term_multiselect_field_args', $args ) );

if ( ! empty( $field['description'] ) ) : ?><small class="description"><?php echo $field['description']; ?></small><?php endif; ?>
No comments yet.

Leave a Reply