Custom Fields
Re-Envisioned
Support MB Admin Columns Sortable custom admin columns?

This topic contains 6 replies, has 2 voices, and was last updated by  Anh Tran 2 years, 6 months ago.

  • Creator
    Topic
  • #4445

    ttroels
    Participant

    Hi

    I tried to create a custom admin column for values who not is in the fields (actually it’s, but I couldn’t create a column ‘name’, if I have fields for ‘first name’ and ‘last name’, so I must concatenate the two strings together)…
    My question is, that I couldn’t create a sortable column with the barebone code you provided (in the bottom at https://metabox.io/docs/mb-admin-columns/). Can you help me?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #4455

    Anh Tran
    Keymaster

    Hi,

    For custom column that you created, you must specify the way to query the data to sort them. You might want to add a hook to pre_get_posts in the class constructor and then filter the query, like this:

    class Prefix_Custom_Admin_Columns extends MB_Admin_Columns_Post {
        function __construct() {
            parent::__construct();
            add_action( 'pre_get_posts', array( $this, 'filter' ) );
        }
        // Other methods
        function filter( $query ) {
            // Sample code to sort the data
            $query->set( 'orderby', 'post_date' );
            $query->set( 'order', 'ASC' );
        }
    }
    #4459

    ttroels
    Participant

    Thank you for the reply. But I can’t make head or tail in the approach. Here is my code. How should I modify this to let me sort the “name” column?

    
    <?php
    class MM_Medlem_Custom_Admin_Columns extends MB_Admin_Columns_Post {
    	/**
    	 * MM_Medlem_Custom_Admin_Columns constructor.
    	 */
    	function __construct() {
    		parent::__construct( 'mm_medlem', array() );
    		add_action( 'pre_get_posts', array( $this, 'filter' ) );
    	}
    
    	public function columns( $columns ) {
    		$columns = parent::columns( $columns );
    
    		/**
    		 * Add more column in a specific position
    		 *
    		 * @param string $position New column position. Empty to not specify the position. Could be 'before', 'after' or 'replace'
    		 * @param string $target The target column. Used with combination with $position
    		 */
    		$position = 'after';
    		$target   = 'mm_ddi-nr';
    		$this->add( $columns, 'navn', 'Navn', $position, $target );
    
    		return $columns;
    	}
    
    	public function show( $column, $post_id ) {
    		switch ( $column ) {
    			case 'navn':
    				$navn = '';
    
    				$fnavn = get_post_meta( $post_id, 'mm_fornavn', true );
    				$mnavn = get_post_meta( $post_id, 'mm_mellemnavn', true );
    				$enavn = get_post_meta( $post_id, 'mm_efternavn', true );
    
    				$navn .= $fnavn;
    				if ( $mnavn ) {
    					$navn .= ' ' . $mnavn;
    				}
    				$navn .= ' ' . $enavn;
    
    				echo $navn;
    				break;
    		}
    	}
    
    	function filter( $query ) {
    		$query->set( 'orderby', 'mm_fornavn' );
    		$query->set( 'order', 'ASC' );
    	}
    }
    
    #4474

    Anh Tran
    Keymaster

    I think the filter should be:

    $query->set( 'orderby', 'meta_value' );
    $query->set( 'meta_key', 'mm_fornavn' );
    $query->set( 'order', 'ASC' );

    For more info about the params, please read the Codex about WP_Query class.

    #4479

    ttroels
    Participant

    Hi again!

    I think we misunderstood each other. What I want is that the column “Navn” is clickable and can be sorted ASC or DESC according to the user interaction, and not let it’s order being defined in backend.

    I solved it myself by digging in your code. The best solution is to apply a filter in the sortable_columnsfunction in base.php:

    public function sortable_columns( $columns ) {
    	foreach ( $this->fields as $field ) {
    		if ( is_array( $field['admin_columns'] ) && ! empty( $field['admin_columns']['sort'] ) ) {
    			$columns[ $field['id'] ] = $field['id'];
    		}
    	}
    	$columns = apply_filters( 'add_sortable_columns', $columns );
    	return $columns;
    }

    and tell my custom.php to add the filter:

    function __construct() {
    	parent::__construct( 'mm_medlem', array() );
    	// Other code
    	add_filter( 'add_sortable_columns', array( $this, 'add_sortable_columns' ) );
    }

    and

    public function add_sortable_columns( $columns ) {
    		$columns['navn'] = 'navn';
    		return $columns;
    }
    #4480

    ttroels
    Participant

    Perhaps you can apply the filter in the next update of Admin Columns, so I still in the future can filter the sortable_columns to add more custom columns which I want to being filtered 🙂

    #4488

    Anh Tran
    Keymaster

    I see!

    Does your code above work? Because if you set your custom column sortable that way, WordPress simply sets orderby=navn and you have to hook to pre_get_posts to filter by that.

    By the way, to add sortable columns, you don’t need to add a new filter to the extension. WordPress has that filter already (that the extension uses). You can refer to this article for more info:

    https://code.tutsplus.com/articles/quick-tip-make-your-custom-column-sortable–wp-25095

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Sortable custom admin columns?’ is closed to new replies.