// Hook into the 'add_meta_boxes' action
add_action('add_meta_boxes', 'custom_post_type_tags_as_checkboxes', 10, 2);
function custom_post_type_tags_as_checkboxes($post_type, $post) {
if ($post_type == 'properties') { // Change 'your_custom_post_type' to your custom post type slug
remove_meta_box('tagsdiv-es_tag', $post_type, 'side'); // Remove the default tags metabox
remove_meta_box('tagsdiv-es_amenity', $post_type, 'side'); // Remove the default tags metabox
remove_meta_box('tagsdiv-es_feature', $post_type, 'side'); // Remove the default tags metabox
remove_meta_box('tagsdiv-es_neighborhood', $post_type, 'side'); // Remove the default tags metabox
remove_meta_box('tagsdiv-es_category', $post_type, 'side'); // Remove the default tags metabox
add_meta_box('tagsdiv-post_tag', __('Tags'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'post_tag'));
// Add custom checkboxes for Amenity taxonomy
add_meta_box('tagsdiv-es_amenity', __('Amenity'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_amenity'));
// Add custom checkboxes for Features taxonomy
add_meta_box('tagsdiv-es_feature', __('Feature'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_feature'));
// Add custom checkboxes for es_neighborhood taxonomy
add_meta_box('tagsdiv-es_neighborhood', __('Neighborhoods'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_neighborhood'));
// Add custom checkboxes for es_category taxonomy
add_meta_box('tagsdiv-es_category', __('Categories'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_category'));
}
}
// Render checkboxes instead of the default tag input
// Render checkboxes with an option to add new terms
function render_tags_as_checkboxes($post, $box) {
$taxonomy = $box['args']['taxonomy'];
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
));
$post_terms = wp_get_post_terms($post->ID, $taxonomy, array('fields' => 'ids'));
if (!empty($terms)) {
echo '<div style="max-height: 150px; overflow-y: auto; border: 1px solid #ddd; padding: 5px;">';
foreach ($terms as $term) {
$checked = in_array($term->term_id, $post_terms) ? ' checked="checked"' : '';
echo '<label style="display: block; margin: 3px 0;"><input type="checkbox" name="tax_input[' . $taxonomy . '][]" value="' . $term->term_id . '"' . $checked . '> ' . esc_html($term->name) . '</label>';
}
echo '</div>';
}
}
// Save the selected terms when the post is saved
add_action('save_post', 'save_custom_tag_checkboxes');
function save_custom_tag_checkboxes($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (isset($_POST['tax_input']['post_tag'])) {
$tag_ids = array_map('intval', $_POST['tax_input']['post_tag']);
wp_set_post_terms($post_id, $tag_ids, 'post_tag');
} else {
wp_set_post_terms($post_id, array(), 'post_tag'); // Clear tags if none are selected
}
// Save Amenity
if (isset($_POST['tax_input']['es_amenity'])) {
$feature_ids = array_map('intval', $_POST['tax_input']['es_amenity']);
wp_set_post_terms($post_id, $feature_ids, 'es_amenity');
} else {
wp_set_post_terms($post_id, array(), 'es_amenity'); // Clear features if none are selected
}
// Save Features
if (isset($_POST['tax_input']['es_feature'])) {
$feature_ids = array_map('intval', $_POST['tax_input']['es_feature']);
wp_set_post_terms($post_id, $feature_ids, 'es_feature');
} else {
wp_set_post_terms($post_id, array(), 'es_feature'); // Clear features if none are selected
}
// Save es_neighborhood
if (isset($_POST['tax_input']['es_neighborhood'])) {
$feature_ids = array_map('intval', $_POST['tax_input']['es_neighborhood']);
wp_set_post_terms($post_id, $feature_ids, 'es_neighborhood');
} else {
wp_set_post_terms($post_id, array(), 'es_neighborhood'); // Clear features if none are selected
}
// Save es_neighborhood
if (isset($_POST['tax_input']['es_category'])) {
$feature_ids = array_map('intval', $_POST['tax_input']['es_category']);
wp_set_post_terms($post_id, $feature_ids, 'es_category');
} else {
wp_set_post_terms($post_id, array(), 'es_category'); // Clear features if none are selected
}
}
No comments:
Post a Comment