Created
April 8, 2025 21:30
-
-
Save g-maclean/45c1f5a62abaec4f0ca32a2bd2f35275 to your computer and use it in GitHub Desktop.
PropertyHive - admin columns modification example - remove Size, add Location
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
// remove size column | |
function remove_size_column($columns) { | |
unset($columns['size']); | |
return $columns; | |
} | |
add_filter('manage_edit-property_columns', 'remove_size_column', 11); | |
// Step 1: Add the Location column to the Property post type | |
add_filter('manage_property_posts_columns', 'add_property_location_column'); | |
function add_property_location_column($columns) { | |
// Add the Location column after the Title column | |
$columns['location'] = __('Location', 'your-text-domain'); | |
return $columns; | |
} | |
// Step 2: Populate the Location column | |
add_action('manage_property_posts_custom_column', 'populate_property_location_column', 10, 2); | |
function populate_property_location_column($column_name, $post_id) { | |
if ($column_name === 'location') { | |
// get all of the terms for custom taxonomy Location | |
$terms = get_the_terms($post_id, 'location'); | |
if ($terms && !is_wp_error($terms)) { | |
$locations = array(); | |
foreach ($terms as $term) { | |
$locations[] = esc_html($term->name); | |
} | |
echo implode(', ', $locations); | |
} else { | |
echo __('', 'propertyhive'); | |
} | |
} | |
} | |
// Step 3: Make the Location column sortable | |
add_filter('manage_edit-property_sortable_columns', 'sortable_property_location_column'); | |
function sortable_property_location_column($columns) { | |
$columns['location'] = 'location'; | |
return $columns; | |
} | |
// Step 4: Modify the query to sort by the Location column | |
add_action('pre_get_posts', 'sort_property_by_location_column'); | |
function sort_property_by_location_column($query) { | |
// Ensure this only affects the admin and the property post type | |
if (!is_admin() || !$query->is_main_query() || $query->get('post_type') !== 'property') { | |
return; | |
} | |
// Adjust the query if sorting by "location" | |
$orderby = $query->get('orderby'); | |
if ($orderby === 'location') { | |
$query->set('orderby', 'location'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment