3

I've added an extra field to a taxonomy called "albums" via this function:

add_action( 'albums_edit_form_fields', 'albums_taxonomy_edit_meta_field', 10, 2 );
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
    $t_id = $term_id;
    $term_meta = get_option( "taxonomy_$t_id" );
    $cat_keys = array_keys( $_POST['term_meta'] );
    foreach ( $cat_keys as $key ) {
        if ( isset ( $_POST['term_meta'][$key] ) ) {
            $term_meta[$key] = $_POST['term_meta'][$key];
        }
    }
    // Save the option array.
    update_option( "taxonomy_$t_id", $term_meta );
}
}

The metadata is saving fine and now I'd like to list or query my taxonomies in a page by displaying next to the taxonomy's name the value of the metadata attached to every taxonomy (in this case, the image). Anybody knows how to do it ?

2
  • 1
    Just for the sake of testing, can you post the full code?
    – brasofilo
    Commented Jul 14, 2012 at 13:48
  • @job78 Any progress on this question?
    – kaiser
    Commented Mar 4, 2013 at 22:36

1 Answer 1

1

How to get the data back

When you look at your code, then you got get_option( "taxonomy_{$t_id}" ) in it, which actually calls the value from the DB options table. So you should simply replace the $t_id with your actual term id.

The term ID can be retrieved via

  • get_query_var( 'tag_id' ) for Tags on tag archive pages
  • get_query_var( 'cat' ) for Categories on cat archive pages
  • get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) )->term_id for custom taxonomy and post format pages.

Why it is NOT recommended to do this!

You're saving one additional entry in your DB options table per tag/term/category/taxon.

This means with a growing number of taxonomy terms, tags or categories, you're quickly polluting your table with stuff that doesn't belong there. Its also not recommended to prefix custom stuff with a generic name like taxonomy_. Try to find this again, when you don't need it anymore. There's also a high risk, that you may catch stuff that doesn't belong to your custom "solution". Always! use a proper prefix like wpse38656_ for DB entries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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