Last active
August 6, 2024 08:58
-
-
Save vishalkakadiya/8c997a5e8789147e012b12cc4af90389 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
This is extremely helpful, thank you !!!