Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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