Home › Forums › General Issues › How to use WP Term Meta, the easy way! › Reply To: How to use WP Term Meta, the easy way!
Hey @edir, thanks for this.. really great. I am using this to update all of my meta fields instead of providing individual filters for each field, the only catch is that each field name has to be prefixed with “meta”. Here’s the code I am using:
/**
* Update term meta based on ACF term meta fields
*/
function namespace_acf_update_term_meta( $value, $post_id, $field ) {
$term_id = intval( filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT ) );
if( substr( $field['name'], 0, 5 ) === 'meta_' ) {
if( $term_id > 0 ) {
update_term_meta( $term_id, $field['name'], $value );
}
}
return $value;
}
add_filter( 'acf/update_value', 'namespace_acf_update_term_meta', 10, 3 );
add_filter( 'acf/update_value', 'namespace_acf_update_term_meta', 10, 3 );
/**
* Load term meta in for ACF meta fields
*/
function namespace_acf_load_term_meta( $value, $post_id, $field ) {
$term_id = intval( filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT ) );
if( substr( $field['name'], 0, 5 ) === 'meta_' ) {
if( $term_id > 0 ) {
$value = get_term_meta( $term_id, $field['name'], $value );
}
}
return $value;
}
add_filter( 'acf/load_value', 'namespace_acf_load_term_meta', 10, 3 );
add_filter( 'acf/load_value', 'namespace_acf_load_term_meta', 10, 3 );
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.