• Resolved onico0

    (@onico0)


    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)
  • Thread Starter onico0

    (@onico0)

    Oops, the solution was in my topic ^_^

    Just copy past this below :

    
    // 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' );
    

    For the travelers who will google something to come here, of course, all this code must be past in your /themes/{your-child-theme}/functions.php file 😉

    Thread Starter onico0

    (@onico0)

    So, to be 100% clear.

    Copy this in your /themes/…/functions.php (nothing more).

    
    /* Show a custom column in the dashboard property listing (a suggestion of @MisterWordPress)  */
    
    // 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');
    
    // 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' );
    
    Plugin Author Merv Barrett

    (@mervb1)

    Thanks for that, we appear to have an issue as the Unique ID column has the sorting option but doesn’t sort as it should. We are working on a fix for that in the next release of Easy Property Listings.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add Sortable Property Column in Admin Dashboard’ is closed to new replies.