WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] custom taxonomy metadata (2 posts)

  1. James
    Member
    Posted 1 year ago #

    I am sure his has been answered somewhere but after 2 days I give up looking so I apologize in adavanced if I missed something obvious.

    I have a custom taxonomy set up that is named 'winery' attached to that taxonomy are several metadata keys 'country', 'address', 'appelation'

    I can easily pull the winery name and description but where I am falling down is trying to get the metadata by using the taxonomy_ID

    What i need is a way to pull the current taxonomy_ID based on the post_ID and then be able to display the fields both one at a time on demand or in an array.

    Any help would be greatly appreciated.

    As an example the following code easily pulls of the taxonomies. But I onyl want the current one.

    <blockquote><?php
    $terms = get_terms("winery", 'number=1');
     $count = count($terms);
     if ( $count > 0 ){
         echo "<ul>";
         foreach ( $terms as $term ) {
           echo "<li>" . $term->term_taxonomy_id . "-" . $term->description . "</li>";</p>   }
         echo "</ul>";
     } ?></blockquote>

    It also doesnt approach the issue of the metadata.

    Thanks.

  2. James
    Member
    Posted 1 year ago #

    Okay since there were no answers I found a solution on my own. What I ended up doing was writing a custom sql query to pull the data I needed and since I had multiple points of data and the query was a bit lengthy I made a function out of it that I could just pass the field names and post ID that I wanted and it would return them. I am now in the process of making them links so they can pull all related posts as well. Anyways here is the function and its use in case it ill help anyone else.

    <?php
    function meta_custom( $myid, $keyto ) {
    global $wpdb;
     $qstr = "
    SELECT $wpdb->taxonomymeta.meta_value
    FROM $wpdb->taxonomymeta, $wpdb->term_taxonomy, $wpdb->term_relationships
    WHERE $wpdb->taxonomymeta.taxonomy_id = $wpdb->term_taxonomy.term_id
    AND $wpdb->term_taxonomy.term_id = $wpdb->term_relationships.term_taxonomy_id
    AND $wpdb->term_relationships.object_id = $myid
    AND $wpdb->taxonomymeta.meta_key = $keyto ";
    $retkey = $wpdb->get_results($qstr);
        foreach ($retkey as $i => $values)  {
            foreach ($values as $key => $value) {
                echo $value;
            }
        }
    }
    ?>

    And it usage in the page.

    <?php meta_custom( $post->ID, "'wregion'" ); ?>

    Note: wregion is the name of the field I am pulling notice the double quotes. Wont work otherwise.

    I am by no means a PHP programmer in fact this is the first piece of PHP code Ive ever written. You know necessity is the mother of invention. ;) Anyways someone else may be able to clean it up a bit.But it works well.

    BTW this code works with the plugin I have for taxonomies which is Ultimate CMS which I highly recommend. Very nice.

Topic Closed

This topic has been closed to new replies.

About this Topic