Forked from vishalkakadiya/manage_columns_wp_admin.php
Created
February 25, 2020 10:36
-
-
Save smartrashed/b9055a20ca3c3f3549dd16fa23f81319 to your computer and use it in GitHub Desktop.
Manage custom columns with sortable in WordPress Admin side
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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