Support

Account

Home Forums General Issues How to use WP Term Meta, the easy way!

Solved

How to use WP Term Meta, the easy way!

  • A couple of things…

    1.
    If I created a field group to show if Taxonomy Term is “myterm” BEFORE the 5.5 update, and that group was enabled, but, if memory serves correct, I never saved any info to that term… is there anything that I need to go and delete/update?
    (I had created the field group in November in anticipation of the 5.5 update coming).
    Bit of a newb on this, but what should I be looking for and where? Visual inspection of wpbc_options and wpbc_termsmeta don’t show anything relating to “myterm” in either – which I think would tally with never having saved any.

    2.
    For the last couple of weeks, I have been seeing a log error pertaining to term meta, as follows…

    [01-Dec-2016 13:51:10 UTC] WordPress database error Table 'brainclo_bgcv10.wpbc_termmeta' doesn't exist for query SELECT term_id, meta_key, meta_value FROM wpbc_termmeta WHERE term_id IN (75) ORDER BY meta_id ASC made by require('wp-blog-header.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), call_user_func_array, WP_Stream\Plugin->init, WP_Stream\Connectors->__construct, WP_Stream\Connectors->load_connectors, WP_Stream\Connector_Menus->get_context_labels, get_terms, WP_Term_Query->query, WP_Term_Query->get_terms, update_termmeta_cache, update_meta_cache

    On occasion, the site has completely died, needing restore from backup, though not clear if this is the cause (this error is also produced without the site dying).

    Now, I could certainly believe this is something to do with the Stream activity-logging plugin I have activated… but I’m also curious if it relates to the term-related activities I have carried out using ACF, ie. creating a field group to load and save data on term meta edit pages (as above).

    Feels like, because ACF has set this field up, somehow Stream is expecting to find data from it?
    Also, the error is quite right that there is no table “wpbc_termmeta” – it is, in fact, “wpbc_termsmeta” (with an “s”).
    Why am I seeing “termSmeta”. Did the ACF DB upgrade script do this?

    Apologies if I’m barking up the wrong tree and should be looking to Stream, but something’s not connecting up.

  • Thanks Edir, you’ve inspired me to write a more general version.
    This version is based on “post_id” provided by ACF, which is actually an ACF-post_id (not a real wp_posts table ID)
    and also based on get_field() documentation, which is pretty steady in ACF-post_id formats.
    (Dis)advantage: uses global variable for taxonomy caching.

    //returns BOOLEAN false or a term id (int)
    $GLOBALS['taxonomies_for_acf_term_meta_filter'] = [];
    function my_acf_get_term_id_from_acf_post_id( $acf_post_id ){
      if( is_numeric($acf_post_id) ) return false; //not a special ID...
    
      if( empty($GLOBALS['taxonomies_for_acf_filter']) )
        $GLOBALS['taxonomies_for_acf_filter'] = get_taxonomies();
      
      $taxonomies = &$GLOBALS['taxonomies_for_acf_filter']; //shorthand
      $term_id = 0;
      foreach( $taxonomies as $taxonomy ){
        //special id is in format somestring_ID
        $term_id = str_replace( $taxonomy.'_', '', $acf_post_id );
    
        //If not numeric at all, or something is removed while cast type to int (must use "!=" !!!!!)
        //=> not a proper ID... => nothing to do here...
        if(  ! is_numeric($term_id) || ( ((int)$term_id) != $term_id )  ) continue;
          
        $term_id = (int)$term_id;
        break;     
      }
      
      if( $term_id < 1 ) return false; //not a proper ID...
      
      return $term_id;
    }
    function my_acf_update_term_meta($value, $post_id, $field) {
      //FILTER! ===> MUST ALWAYS RETURN $value !
    
      $term_id = my_acf_get_term_id_from_acf_post_id( $post_id );
      if( $term_id === false ) return $value; //not a proper ID... MUST USE "===" !!!!!
      
      update_term_meta($term_id, $field['name'], $value);
    	
      return $value;
    }
    add_filter('acf/update_value', 'my_acf_update_term_meta', 10, 3);
    add_filter('acf/update_value', 'my_acf_update_term_meta', 10, 3);
    
    function my_acf_load_term_meta($value, $post_id, $field) {
      //FILTER! ===> MUST ALWAYS RETURN $value !
    
      $term_id = my_acf_get_term_id_from_acf_post_id( $post_id );
      if( $term_id === false ) return $value; //not a proper ID... MUST USE "===" !!!!!
    	
      $value = get_term_meta($term_id, $field['name'], true);
    	
      return $value;
    }
    add_filter('acf/load_value', 'my_acf_load_term_meta', 10, 3);
    add_filter('acf/load_value', 'my_acf_load_term_meta', 10, 3);
  • ACF5 does support term meta now, hopefully a free version will be released on WP soon. From what I understand, the developer is working on it.

Viewing 3 posts - 26 through 28 (of 28 total)

The topic ‘How to use WP Term Meta, the easy way!’ is closed to new replies.