Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

So, questions are:

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

4 Answers 4

The coordinates could be added using metaboxes. Check out the example here http://codex.wordpress.org/Function_Reference/add_meta_box#Example

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
    
The coordinates wouldn't be attached to the event, they would be attached to the venue - which is assigned to the event, the venue itself would have different properties - like website and coordinates, a description etc –  pax Nov 24 '11 at 21:27
    
I think we agree, in that case. sorry if that's not clear from the above. –  djb Nov 24 '11 at 22:41

As others have already said, no, terms can not have additional properties attached to them. A term in a taxonomy is just a label for a group of items.

So, I would instead make venue a second custom post type and then connect it to the event post type using Posts 2 Posts: http://wordpress.org/extend/plugins/posts-to-posts/

share|improve this answer
    
Problem is? (how) can you then search for venues? - display all events that take place in a certain venue, like you could do with a taxonomy term (tags/categories)? –  pax Nov 30 '11 at 0:01
1  
That's what P2P is all about. The only thing it doesn't have out of the box yet is rewrite rules. –  scribu Nov 30 '11 at 11:57
    
So the URLs would look like this: http://example.com/?connected_type=events-to-venues&connected_items=123 where 123 is the id of a venue. But you can add your own rewrite rules to make them prettier, say http://example.com/venue/some-venue-slug/events/ –  scribu Nov 30 '11 at 12:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.