Skip to content

Instantly share code, notes, and snippets.

@cfxd
Last active September 27, 2015 14:14
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 cfxd/8e7927848100d4cd4d6d to your computer and use it in GitHub Desktop.
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
<?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');
<?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);
<?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');
<?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