Home › Forums › General Issues › ACF Taxonomy Filtering › Reply To: ACF Taxonomy Filtering
I’m a little late to this party but I was at my wits’ end figuring this out and managed to nail it.
I have a custom taxonomy called issue
which has a custom field issue_date
(date picker field). I wanted to add a custom column in the taxonomy edit screen to sort my issues by their dates. Here is what I came up with:
Step 1: Add the new Date column
function issue_columns($issue_columns) {
$issue_columns['date'] = 'Date';
return $issue_columns;
}
add_filter('manage_edit-issue_columns', 'issue_columns');
Step 2: Populate the new Date column
function manage_issue_columns($out, $column_name, $term_id) {
$issue = get_term($term_id, 'issue');
$date = DateTime::createFromFormat('Ymd', get_field('issue_date', $issue));
switch($column_name) {
case 'date':
$out = $date->format('Y/m/d');
break;
default:
break;
}
return $out;
}
add_filter('manage_issue_custom_column', 'manage_issue_columns', 10, 3);
Step 3: Make the new Date column sortable
function my_sortable_issue_column($columns) {
$columns['date'] = 'issue_date';
return $columns;
}
add_filter('manage_edit-issue_sortable_columns', 'my_sortable_issue_column');
Step 4: Define the custom sorting (where the magic happens)
function sort_issues_by_date_column($pieces, $tax, $args) {
global $pagenow;
if(!is_admin()) {
return;
}
if(is_admin() && $pagenow == 'edit-tags.php' && $tax[0] == 'issue' && (!isset($_GET['orderby']) || $_GET['orderby'] == 'issue_date')) {
$pieces['join'] .= " INNER JOIN wp_options AS opt ON opt.option_name = concat('issue_',t.term_id,'_issue_date')";
$pieces['orderby'] = "ORDER BY opt.option_value";
$pieces['order'] = isset($_GET['order']) ? $_GET['order'] : "DESC";
}
return $pieces;
}
add_filter('terms_clauses', 'sort_issues_by_date_column', 10, 3);
For reference, my date picker field setting returns the date in the format Ymd
.
If anyone has any improvements or questions please let me know!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.