Home › Forums › General Issues › ACF Taxonomy Filtering
Hi,
I’m interested in this :
http://old.support.advancedcustomfields.com/discussion/5322/get_terms-by-meta/p1
I’m working with ACF and Taxonomies, and I don’t find, in the documentation, something that allows me to filter the Taxonomies by the values stored on the wp_options table.
It could by something like this, but with Taxonomies:
http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/
I have a taxonomy called “Person”, and I have many fields. For example, I would like to filter by sex and country.
Is there any function that allows me to do this? Or should I work with WP-Query?
Thanks in advance
Hi @funack
I am yet to find a solution for this.
As discussed in the old support thread:
Term meta is stored in the wp_options table (as there is no ‘default’ meta table for terms). Each option_name is prefixed with the taxonomy name + term_id
Like this:
category_2_image => 123
I don’t think it is possible to perform a meta query due to the way data is stored.
Perhaps you could look at another term_meta solution?
Hi,
I’m just finding myself in the same situation. (about 1,5 years later.. )
Luckily for me my needs are in the wp admin backend so performance is not crucial.
The only solution I can think of is to fetch all terms, loop through them and check for the value using get_field. Not pretty and wont work when you have thousands of terms but for a smaller amount and in backend it should be fine.
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!
Cool 🙂 Thanks for the share @cfxd
Also, there are now native term meta on the horizon in WPs development so I’m looking forward to seeing that!
The topic ‘ACF Taxonomy Filtering’ is closed to new replies.
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.