Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Simple Term Meta Plugin - Display Taxonomy Meta WordPress

  • SOLVED

I am using the Simple Term Meta plugin to allow extra meta data for taxonomies:
http://wordpress.org/extend/plugins/simple-term-meta/

I have a multisite setup with taxonomies that are shared across all blogs and so have altered the plugin very slightly to allow it to create tables for each blog, and all appears to work just fine - I can create new fields for taxonomies in a master custom functions file that is shared by all the blogs and the fields show in the backend and save just in the database.

I have been using this guide so far to assist with the setup:
http://www.wpmods.com/adding-metadata-taxonomy-terms/

All parts of the plugin appear to work as expected and I can get extra fields to show on the frontend for the original categories using php code such as:

<?php $thumb_src = get_term_meta(get_query_var('cat'), 'image-url', true); ?>
<?php if($thumb_src != '') : ?>
<img src="<?php echo $thumb_src; ?>" width="50" height="50" style="float:left; margin-right: 10px;" />
<?php endif; ?>


OR

<?php echo get_term_meta(get_query_var('cat'), 'extra-field-data', true); ?>

I am trying to get this working for custom taxonomies and have altered the functions slightly so that instead of 'categories' they relate to the custom taxonomy names, for example for a taxonomy called 'stores': (this shows the new fields on each blog taxonomy and saves correctly into the database - image-url and image2 are simply test fields in this example)


add_action('stores_add_form_fields', 'stores_metabox_add', 10, 1);
add_action('stores_edit_form_fields', 'stores_metabox_edit', 10, 1);

function stores_metabox_add($tag) { ?>
<h3>Extra Merchant Information</h3>
<div class="form-field">
<label for="image-url"><?php _e('Image URL') ?></label>
<input name="image-url" id="image-url" type="text" value="" size="40" aria-required="true" />
<p class="description"><?php _e('This image will be the thumbnail shown on the category page.'); ?></p>
</div>
<div class="form-field">
<label for="image2"><?php _e('Image2') ?></label>
<input name="image2" id="image2" type="text" value="" size="40" aria-required="true" />
<p class="description"><?php _e('This image will be the thumbnail shown on the category page.'); ?></p>
</div>
<?php }

function stores_metabox_edit($tag) { ?>
<h3>Extra Merchant Information</h3>
<table class="form-table">
<tr class="form-field">
<th scope="row" valign="top">
<label for="image-url"><?php _e('Image URL'); ?></label>
</th>
<td>
<input name="image-url" id="image-url" type="text" value="<?php echo get_term_meta($tag->term_id, 'image-url', true); ?>" size="40" aria-required="true" />
<p class="description"><?php _e('This image will be the thumbnail shown on the category page.'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="image2"><?php _e('Image2'); ?></label>
</th>
<td>
<input name="image2" id="image2" type="text" value="<?php echo get_term_meta($tag->term_id, 'image2', true); ?>" size="40" aria-required="true" />
<p class="description"><?php _e('This image will be the thumbnail shown on the category page.'); ?></p>
</td>
</tr>
</table>
<?php }



add_action('created_stores', 'save_stores_metadata', 10, 1);
add_action('edited_stores', 'save_stores_metadata', 10, 1);

function save_stores_metadata($term_id){
{

if (isset($_POST['image-url']))
update_term_meta( $term_id, 'image-url', $_POST['image-url']);
}
{
if (isset($_POST['image2']))
update_term_meta( $term_id, 'image2', $_POST['image2']);
}
}


The problem is when I want to show meta data from the custom taxonomy terms. Once again all the data is stored correctly in the database in the same table as for the category extra fields, but no matter what I try, I can't get the meta data to display on the frontend of the site!

I have been trying variations of:
<?php echo get_term_meta(get_query_var('term'), 'image2', true); ?>

Please can someone help with getting thesis values to display.

Answers (2)

2011-11-09

John Cotton answers:

For a start, have you checked with the query var actually exists on the page you're looking at?

global $wp; print_r($wp);

somewhere are the header will tell you.

I suspect it doesn't (I might be wrong, but I think you only get it on a term archive page).

If it does exist, is it an ID of one of your terms? That's the next thing to check.

Finally - and I don't know this plugin - so I'm just wondering how it knows which taxonomy you're querying. If you've got multiple taxonomies with meta data the normal thing to do would be have each set in a different table (a real pain which I hope WordPress will change some day soon!).


John


robster comments:

Hi John,

Sorry i'm not fully up to speed here and not really sure how to check for query var, but the page it's being queried on is a term archive and on a standard category archive i can get extra term meta to display using the above method/code.

As far as I know the plugin just creates the following extra functions:
add_term_meta, delete_term_meta, get_term_meta, update_term_meta, get_term_custom, get_term_custom_keys, get_term_custom_values

The plugin also creates a table for each blog called 'termmeta'

This table for each blog stores the extra data for each field in columns called:
1 - meta_id (id for the meta)
2 - term_id (term id from the individual blog)
3 - meta_key (such as 'image-url' in the example above)
4 - meta_value (actual meta information)

I would guess that the extra meta is queried using the following function:
<?php echo get_term_meta(get_query_var('cat'), 'image2', true); ?>

And this in turn looks up the relevant info with respect to the current category term (or taxonomy term)

I'm basically using the plugin to created the tables simply and quickly, and then defining custom functions to create the extra field inputs and save functions.

i am certainly open to suggestions if there is another simple way to create the tables, meta boxes etc. for each site and each shared taxonomy and to display the information?


John Cotton comments:

OK, change that line to:


<?php

$term = get_query_var('my_custom_taxonomy_name');
echo 'term is: '.print_r($term, true);

$term = get_term_by('name', $term, 'my_custom_taxonomy_name')

$meta = get_term_meta($term->ID, 'image2', true);

echo 'meta is: '.print_r($meta, true);
?>


(You'll need to replace my_custom_taxonomy_name with whatever the name really is).

What do you get?


robster comments:

Hi John,

Using this:
<?php
$term = get_query_var('stores');
echo 'term is: '.print_r($term, true);
$term = get_term_by('name', $term, 'stores');
$meta = get_term_meta(get_query_var('stores'), 'image2', true);
echo 'meta is: '.print_r($meta, true);
?>


This is outputting as:

<em>term is: amazonmeta is:</em>

(with amazon being the current term and stores being the custom taxonomy)

So looks like it's displaying the basic meta fine, but not the extra fields...


John Cotton comments:

You've not copied it quite right....

Your code should read:


<?php

$term = get_query_var('stores');

echo 'term is: '.print_r($term, true);

$term = get_term_by('name', $term, 'stores');

$meta = get_term_meta($term->ID, 'image2', true);

echo 'meta is: '.print_r($meta, true);

?>


robster comments:

Apologies - tried both and still exactly the same result...


John Cotton comments:

Have you looked in the database and checked that there is a row with the term id for amazon and the meta_key 'image2'?


robster comments:

Yep just checked and the row definitely exists:

term_id is '3' (amazon term id)
meta_key is 'image2'
meta_value is 'this is field 2'

Have attached a screenshot to show this in case i'm missing something...