Can custom taxonomies items have attached properties?

Let’s say for (a classic) example that I want to create an event custom post type, which will have assigned some specific fields/properties. One of them: location venue.

Location venue, which I’m thinking should be a tag in a custom taxonomy, would have, besides the title, also an url and a pair of coordinates, for example.

Read More

So, questions are:

  1. Would this be a sensible approach?
  2. How would I go implementing it?

Related posts

Leave a Reply

4 comments

  1. This sounds like a good idea as it would let you query by location etc. I don’t know what you plan to use the co-ordinates for, but it makes no sense to have them attached to an event (unless that event is a one-off in the middle of a field, but I guess that would be an edge case?).

    As for implementation, this is a very useful plugin:

    http://wordpress.org/extend/plugins/taxonomy-metadata/

    It allows you to add/get/update/delete term meta just as though it were post meta

    So you could do something like this (just to give the idea, may not work 100%) :

      add_action( 'locations_edit_form_fields', 'edit_locations');
      function edit_locations($location)
      {
        $url = get_term_meta($location->term_id, 'url', true); 
        ?>
       <tr class="form-field">
          <th scope="row" valign="top"><label for="url">url</label></th>
          <td>
            <input type="text" name="url" id="url" 
                value="<?php echo $url; ?>"/>
            <p class="description">Add url here.</p>
          </td>
      </tr> 
      }
    

    to set the information, and then use $_POST to update the meta thusly:

     add_action( 'edited_locations', 'update_location', 10, 2);
     function update_location($location_term_id)
     {
       if (!$location_term_id) 
           return;
    
       if (isset($_POST['url'])) {
          //you may wish to sanitize this value (not sure if it has been already?)
          update_term_meta($location_term_id, 'url', $_POST['url']);
       }
     }
    

    Further reading: http://en.bainternet.info/2011/custom-taxonomies-extra-fields

  2. What you’re wanting is term meta data, e.g. two events are tagged at “The Big place”, and “The Big Place” has coordinates etc attached to it, that are pulled in by the two events.

    Unfortunately WordPress does not have a facility for term meta. You can add it via plugins, alternatively, you could have a post type location-meta, set up to not be public and not show in public listings, that you can attach the meta to, then assing it to relevant term tag.

    Or you could make your taxonomy heirarchical, and add child terms with names such as ‘coordinates-X-Y-Z’ etc and filter them out when viewing the taxonomy terms.