How do I display a custom field from a custom taxonomy in single.php?

I know that the following mysql query produces the exact results I wish to echo within single.php:

SELECT meta_value
FROM `wp_taxonomymeta`
WHERE `taxonomy_id` =565
AND `meta_key` LIKE 'baseurl'

First, I need to be able to use $wpdb to output that result, and no matter what I try I can’t get it to work. My final try:

Read More
<?php global $wpdb;
    $toc = $wpdb->get_row("
SELECT meta_value
FROM `wp_taxonomymeta`
WHERE `taxonomy_id` =565
AND `meta_key` LIKE 'baseurl'
"); 
    echo $toc; ?>

produced the following error:

Catchable fatal error: Object of class stdClass could not be converted to string in /wp-content/themes/new/single.php on line 22

Second… and it’s important. The value 565 I am using for the taxonomy_id was only for testing… the actual value I need to insert in place of the number 565 can be displayed in single.php using:

<?php $term = get_term_by('id', $termID, 'dataset'); echo $term->term_id; ?>

For reference, so you can understand what I’m trying to do… I have assigned a custom field to a taxonomy and am trying to get the results of that custom field to display in single.php… while I found a plugin (Ultimate CMS) that would do this for me, it also chooses to display a filter for all taxonomies at the top of my Post Screen… when you have 50,000+ rows in your taxonomy that causes way to much page load in the admin, and I can’t find a way to suppress it.

Any help with my NOOB try at coding would be appreciated!

Thanks

Related posts

Leave a Reply

1 comment

  1. Catchable fatal error: Object of class stdClass could not be converted
    to string in /wp-content/themes/new/single.php on line 22

    This is because $wpdb->get_row returns an object by default and you can’t echo an object. Changing echo $toc to echo $toc->meta_value will give you your desired result.

    Your other problem is that multiple terms can have have the same term_id. Your custom table should be using the term_taxonomy_id which will always be unique.

    global $wpdb;
    $term = get_term_by('id', $termID, 'dataset');  //Seems redundant does $term->term_id === $termID???
    $query = $wpdb->prepare( "SELECT * FROM {$wpdb->taxonomymeta}
                              WHERE `taxonomy_id` = %d
                              AND `meta_key` LIKE 'baseurl' ",
                              $term->term_id );
    $toc = $wpdb->get_row( $query );
    echo $toc->meta_value;