Custom taxonomy with meta data

This snippet shows how you can add metadata to your existing custom taxonomy. So you can use updatemetadata() and getmetadata() on your taxonomy. The filter on switchblog is only necessary if the code runs on a blog network (recommended to implement anyway). The name of the database table is very important, if you work with this code you probably want to search and replace "taxonomymeta" everywhere with "<name of your taxonomy>meta". You also need to replace taxonomyid with <name of your taxonomy>_id.

Raw
<?php

class Taxonomy {

        /**   
         * Register taxonomy metadata
         *
         * We use the taxonomy metadata for color info right now and later for
         * subscription status etc. Run this code after the taxonomy 'taxonomy'
   * has been registered.
         *
   * @fixme The code to create the table should only run once
   * @todo the switch_blog action has always to be hooked into
   *
         * @return none
         */
        function add_taxonomy_meta() {
                $this->create_taxonomy_meta_table();
                $this->update_wpdb();
                add_action( 'switch_blog', array( &$this, 'update_wpdb' ) );
        }     

        /**   
         * WordPress doesn't seem to support metadata on custom taxonomies out
         * of the box, we need to update $wpdb->taxonomy to the correct table
         * ourself.
         *
         * @return none
         */
        function update_wpdb() {
                global $wpdb;
                $wpdb->taxonomymeta = $wpdb->prefix . 'taxonomymeta';
        }     

        /**   
         * Create a table for taxonomy meta
         *
         * @return none
         */
        function create_taxonomy_meta_table() {
                if ( $this->taxonomy_meta_table_exists() ) {
                        return;
                }     
                global $wpdb;
                $query = "CREATE TABLE `{$wpdb->prefix}taxonomymeta` (
                           `meta_id` bigint(20) unsigned not null auto_increment,
                           `taxonomy_id` bigint(20) unsigned not null default '0',
                           `meta_key` varchar(255),
                           `meta_value` longtext,
                           PRIMARY KEY (`meta_id`),
                           KEY `taxonomy_id` (`taxonomy_id`),
                           KEY `meta_key` (`meta_key`)
                        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2;";
                $r = $wpdb->query( $wpdb->prepare( $query ) );
        }     

        /**   
         * Check if the taxonomy meta table exists
         *
         * @return bool table exists
         */
        function taxonomy_meta_table_exists() {
                global $wpdb;
                $query = "SHOW TABLES LIKE '{$wpdb->prefix}taxonomymeta';";
                $indexes = $wpdb->get_var( $wpdb->prepare( $query ) );
                if ( $indexes )
                        return true; 
                return false;
        }     

}
?>

2 comments

  1. avatar
    wrote this comment on
    Hello Nicolas! Where should I insert this code to use custom taxonomy? Thanks.
  2. avatar
    wrote this comment on
    Well, this belongs into a theme or a plugin. You might want to try to code something simpler if you're just beginning WordPress coding.

Reply

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo