Support

Account

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 );