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.

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:

<?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

share|improve this question
    
How are you assigning the custom field to the taxonomy? –  brasofilo Feb 3 '13 at 4:12
    
@brasofilo The taxonomy_id in the custom field (wp_taxonomymeta table) is equal to the term_id in the taxonomy (term_taxonomy table). –  perdrix Feb 3 '13 at 4:21
add comment

1 Answer 1

up vote 0 down vote accepted

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;
share|improve this answer
    
Thanks for the reply! I'll admit I'm never too old to learn something new, and you've taught me a couple of things here. The table I was using was actually created by the plugin initially referenced at the top... so I'll create my own table using the term_taxonomy_id instead. I'm going to edit your code above to show the resulting code that ended up working. I had to slightly tweak yours to make sure it referenced the prefix for the table, irregardless of what it was. I also removed your get_term_by as it isn't needed (which is what you thought). –  perdrix Feb 3 '13 at 6:33
add comment

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.