Sortable custom admin columns?
Support › MB Admin Columns › Sortable custom admin columns?
- This topic has 6 replies, 2 voices, and was last updated 8 years, 5 months ago by
Anh Tran.
-
AuthorPosts
-
November 7, 2016 at 6:15 AM #4445
ttroels
ParticipantHi
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?November 9, 2016 at 8:35 AM #4455Anh Tran
KeymasterHi,
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' ); } }
November 9, 2016 at 4:19 PM #4459ttroels
ParticipantThank 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' ); } }
November 12, 2016 at 9:36 AM #4474Anh Tran
KeymasterI 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.
November 13, 2016 at 3:21 PM #4479ttroels
ParticipantHi 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_columns
function 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; }
November 13, 2016 at 3:23 PM #4480ttroels
ParticipantPerhaps 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 🙂November 15, 2016 at 4:50 PM #4488Anh Tran
KeymasterI 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 topre_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
-
AuthorPosts
- The topic ‘Sortable custom admin columns?’ is closed to new replies.