Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save harrilehtisaari/394d3ee57bc35eaf2548c738ab90ee98 to your computer and use it in GitHub Desktop.

Select an option

Save harrilehtisaari/394d3ee57bc35eaf2548c738ab90ee98 to your computer and use it in GitHub Desktop.
Save ACF-fields in taxonomy terms as term_meta
/**
* Save all acf fields in terms as term meta (for easy query purposes [get_terms(meta_query)])
* Field names have to be prefixed with "meta_"
* URL: https://support.advancedcustomfields.com/forums/topic/how-to-use-wp-term-meta-on-acf-the-easy-way/
* 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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment