Home › Forums › General Issues › How to use WP Term Meta, the easy way! › Reply To: How to use WP Term Meta, the easy way!
I’m of a different opinion. The way I look at this, the only purpose for storing anything in term_meta
until ACF does it is to have those values in a place to make it easy to search for terms using get_terms()
. I don’t see any point in slowing down the admin to put everything into the term meta table or going to the extra effort of getting it from there, or making the effort of deleting anything from the options table. All of these things will just slow down the site simply to keep the database clean.
Since trying to search for things where serialized data is being stored or when the field is a repeater or flex field can be ridiculous I don’t see any point in putting this type of data into the term meta table. Anything that I’m going to want to use it get_terms()
is going to be a simple field type. More complicated fields I’d create a custom function just for that field.
Anyway, here’s what I’m using.
<?php
add_fitler('acf/update_value', 'temp_acf_update_term_meta', 10, 3);
function temp_acf_update_term_meta($value, $post_id, $field) {
if (is_array($value) || is_object($value) || acf_is_sub_field($field)) {
return $value;
}
$object = get_queried_object();
$term_id = intval(filter_var($post_id, FILTER_SANITIZE_NUMBER_INT));
if (!isset($object->taxonomy) || !isset($object->term_id) || $term_id != $object->term_id) {
// the current object is not a term
return $value;
}
// good value to store
update_term_meta($term_id, $field['name'], $value);
return $value;
} // end function temp_acf_update_term_meta
?>
I’m also not in an all fired hurry for ACF to eliminate using the options table for term meta anyway because I have not used ACF function in theme templates in a long time opting to use get_post_meta()
and get_option()
instead. This came about because I build plugins for ACF and using ACF functions in a plugin that accesses data using these functions can cause unintended side effects, like infinite loops and getting the wrong values. When ACF does implement term meta I’m going to need to figure out how to hook into get_option()
and divert it to get_term_meta()
when the field name starts with a taxonomy name. I’m sure it can be done, I’m just not in a hurry to get there. I figure I have some time though, knowing the way ACF is built around {$taxonomy}_{$term_id}
for these option names I think is one the reasons it hasn’t been implemented in ACF yet.
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.