Last active
September 27, 2015 14:14
-
-
Save cfxd/8e7927848100d4cd4d6d to your computer and use it in GitHub Desktop.
Create a Custom, Sortable Column for a Taxonomy. See http://cfxdesign.com/create-a-custom-sortable-column-for-a-taxonomy
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 create_date_column_for_issues($issue_columns) { | |
$issue_columns['date'] = 'Date'; | |
return $issue_columns; | |
} | |
add_filter('manage_edit-issue_columns', 'create_date_column_for_issues'); |
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 populate_date_column_for_issues($value, $column_name, $term_id) { | |
$issue = get_term($term_id, 'issue'); | |
$date = DateTime::createFromFormat('Ymd', get_field('issue-date', $issue)); | |
switch($column_name) { | |
case 'date': | |
$value = $date->format('Y/m/d'); | |
break; | |
default: | |
break; | |
} | |
return $value; | |
} | |
add_filter('manage_issue_custom_column', 'populate_date_column_for_issues', 10, 3); |
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 register_date_column_for_issues_sortable($columns) { | |
$columns['date'] = 'issue_date'; | |
return $columns; | |
} | |
add_filter('manage_edit-issue_sortable_columns', 'register_date_column_for_issues_sortable'); |
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 sort_issues_by_date($pieces, $taxonomies, $args) { | |
global $pagenow; | |
if(!is_admin()) { | |
return $pieces; | |
} | |
if(is_admin() && $pagenow == 'edit-tags.php' && $taxonomies[0] == 'issue') { | |
$orderby = isset($_REQUEST['orderby']) ? trim(wp_unslash($_REQUEST['orderby'])) : 'issue-date'; | |
$order = isset($_REQUEST['order']) ? trim(wp_unslash($_REQUEST['order'])) : 'DESC'; | |
if($orderby == 'issue-date') { | |
$pieces['join'] .= " INNER JOIN wp_options AS opt ON opt.option_name = concat('issue_',t.term_id,'_".$orderby."')"; | |
$pieces['orderby'] = "ORDER BY opt.option_value"; | |
$pieces['order'] = $order; | |
} | |
} | |
return $pieces; | |
} | |
add_filter('terms_clauses', 'sort_issues_by_date', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment