Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smartrashed/b9055a20ca3c3f3549dd16fa23f81319 to your computer and use it in GitHub Desktop.
Save smartrashed/b9055a20ca3c3f3549dd16fa23f81319 to your computer and use it in GitHub Desktop.
Manage custom columns with sortable in WordPress Admin side
<?php
/*
* =================================================================================================
* Below both hooks works for custom post types.
* e.g. Suppose we have custom post-type name : Books
* @ref https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
* =================================================================================================
*/
/* Add custom column to book list */
function add_sticky_column( $columns ) {
return array_merge( $columns,
array( 'sticky' => __( 'Sticky', 'your_text_domain' ) ) );
}
add_filter( 'manage_book_posts_columns' , 'add_sticky_column' );
/* Add content to above added custom column */
function book_posts_stickiness( $column, $post_id ) {
if ($column == 'sticky'){
// Your custom code goes here
}
}
add_action( 'manage_book_posts_custom_column' , 'book_posts_stickiness', 10, 2 );
/*
* =================================================================================================
* After adding custom column above it's time to SORT of that field.
* @ref https://code.tutsplus.com/articles/quick-tip-make-your-custom-column-sortable--wp-25095
* =================================================================================================
*/
// make it sortable
function book_sortable_columns( $columns ) {
$columns['slices'] = 'slice';
return $columns;
}
add_filter( 'manage_edit-book_sortable_columns', 'book_sortable_columns' );
function book_slice_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'slice' == $orderby ) {
$query->set('meta_key','slices');
$query->set('orderby','meta_value_num'); // "meta_value_num" is used for numeric sorting
// "meta_value" is used for Alphabetically sort.
// We can user any query params which used in WP_Query.
}
}
add_action( 'pre_get_posts', 'book_slice_orderby' );
/*
* =================================================================================================
* Below both hooks works for built in (posts) post-type only.
* @ref https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
* =================================================================================================
*/
/* Add custom column to post list */
function add_sticky_column( $columns ) {
return array_merge( $columns,
array( 'sticky' => __( 'Sticky', 'your_text_domain' ) ) );
}
add_filter( 'manage_posts_columns' , 'add_sticky_column' );
/* Add content to above added custom column */
function display_posts_stickiness( $column, $post_id ) {
if ($column == 'sticky'){
// Your custom code goes here
}
}
add_action( 'manage_posts_custom_column' , 'display_posts_stickiness', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment