Add Sortable Property Column in Admin Dashboard
-
Hello,
I searched a while how to add a sortable custom column (for me : property_unique_id) in the edit posts property listing dashboard.
Below, my first try, and then, my code which almost works
if(false) { /* NOT WORKS, SEE BELOW */ /* Adding Sortable Column in Dashboard | Source (inspiration) : https://gist.github.com/ethanhinson/7419385 */ function property_id_column_register( $columns ) { // Insert property ID in between date/title $last = array_pop($columns); $columns['property_unique_id'] = 'Réf'; $columns[strtolower($last)] = $last; var_dump($columns); return $columns; } add_filter( 'manage_property_posts_columns', 'property_id_column_register' ); function property_id_column_display( $column_name, $post_id ) { echo 'REF'; if ( 'property_unique_id' != $column_name ) return; $property_unique_id = get_post_meta($post_id, 'property_unique_id', true); if ( !$property_unique_id ) $property_unique_id = '<em>No ID entered!</em>'; echo $property_unique_id; } add_action( 'manage_property_posts_custom_column', 'property_id_column_display', 10, 2 ); // Register the column as sortable function property_id_column_register_sortable( $columns ) { $columns['property_unique_id'] = 'property_unique_id'; return $columns; } add_filter( 'manage_edit-property_sortable_columns', 'property_id_column_register_sortable' ); // Make our sorting work function property_id_column_orderby( $vars ) { if ( isset( $vars['orderby'] ) && 'property_unique_id' == $vars['orderby'] ) { $vars = array_merge( $vars, array( 'meta_key' => 'property_unique_id', 'orderby' => 'meta_value' ) ); } return $vars; } add_filter( 'request', 'property_id_column_orderby' ); } // FROM HERE IT WORKS !!! /* Show a custom column in the dashboard property listing (a suggestion of @MisterWordPress, it [almost] works) */ // Add the column heading function epl_manage_property_columns_heading_custom( $columns ) { $columns['property_unique_id'] = __('Property ID', 'easy-property-listings' ); return $columns; } add_filter( 'manage_edit-property_columns', 'epl_manage_property_columns_heading_custom' ); // Put the value inside function property_id_column_display( $column_name, $post_id ) { if ( 'property_unique_id' != $column_name ) return; $property_unique_id = get_post_meta($post_id, 'property_unique_id', true); if ( !$property_unique_id ) $property_unique_id = '<em>No ID entered!</em>'; echo $property_unique_id; } add_action( 'manage_property_posts_custom_column', 'property_id_column_display', 10, 2 ); // Make it sortable function epl_manage_listings_sortable_columns_custom( $columns ) { $columns['property_unique_id'] = 'property_unique_id'; return $columns; } add_filter( 'manage_edit-property_sortable_columns', 'epl_manage_listings_sortable_columns_custom');
My only one concern is that my sortable column do not sort properly. It sort by something, but not by my property_unique_id… :-)) any idea to share ?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Add Sortable Property Column in Admin Dashboard’ is closed to new replies.