Last active
February 27, 2017 16:43
-
-
Save gradosevic/43dd03525c2e82e2ea450490ca4ce0e7 to your computer and use it in GitHub Desktop.
WordPress: custom post type column, create, link and sort by
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 | |
function manage_mypostype_custom_column( $column, $post_id ) { | |
global $post; | |
switch( $column ) { | |
case 'category': | |
$terms = get_the_terms( $post_id, 'mypostype-category' ); | |
if($terms[0]){ | |
$theCategory = $terms[0]->name; | |
$termID = $terms[0]->term_id; | |
echo "<a href='/wp-admin/term.php?taxonomy=mypostype-category&tag_ID=".$termID."&post_type=mypostype'>".$theCategory."</a>"; | |
} | |
break; | |
/* Just break out of the switch statement for everything else. */ | |
default : | |
break; | |
} | |
} | |
add_action( 'manage_mypostype_posts_custom_column', 'manage_mypostype_custom_column', 10, 2 ); | |
function manage_mypostype_columns( $columns ) { | |
return [ | |
'cb' => '<input type="checkbox" />', | |
'title' => 'Title', | |
'category' => 'Category', | |
'date' => 'Date' | |
]; | |
} | |
add_filter( 'manage_mypostype_posts_columns', 'manage_mypostype_columns' ); | |
// Make these columns sortable | |
function sortable_edit_mypostype_columns($columns) { | |
$columns['category'] = 'mypostype-category'; | |
return $columns; | |
} | |
add_filter( "manage_edit-mypostype_sortable_columns", "sortable_edit_mypostype_columns" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment