Home › Forums › General Issues › Query taxonomies with a acf field
Hello
I was trying to create a query to retrieve all taxonomies with a custom field created via the gui in the back-end. I tried to use the wp get_terms and the WP_Term_Query but both of them returned an empty result every time. Upon a inspection of my wordpress database I noticed that the values and the field itself of the taxonomy was not been created in the _termmeta table, which i find very odd since it would be the logical spot that the field information should be stored (at this time I still have not found where the data of the field is stored), but I’m sure is somewhere in the database, since the query with the function get_field('field_name', 'taxonomy_name_term_id', true)
manages to retrieve the field value successfully.
I might be doing something wrong on the creation of the field which causes the field values not been stored in the correct table (although I don’t see how, since the field is displayed in the taxonomies that I want it to).
Here is the configuration of the field created:
The rest of the configurations are set as the default value, so I don’t think is worth mentioning.
As for the query of the taxonomies itself I did the following :
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_key' => 'featured-product-category',
'meta_value' => '1',
)
);
I’m pretty sure the function is been called and that the taxonomy exists, as i did the query without the meta information and returned all the taxonomies successfully. I guess I could just loop through all categories checking for the meta value with the get_field function() but I don’t think is the best way to go.
I’m not sure if this is how the plugin is supposed to work or if i’m doing something wrong so I appreciate any comments about this subject or maybe a better workaround (the correct way to make the query).
Thank you
Term meta will be used in ACF 5.5 which is due to be released, as far as I know, by the end of the year. If this is needed before then see this post https://support.advancedcustomfields.com/forums/topic/how-to-use-wp-term-meta-on-acf-the-easy-way/ for some temporary solutions.
Thank you. That post is just what i needed. I’m going to use one of the solutions on that post.
The topic ‘Query taxonomies with a acf field’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.