1

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

2
  • How are you assigning the custom field to the taxonomy?
    – brasofilo
    Commented Feb 3, 2013 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
    Commented Feb 3, 2013 at 4:21

1 Answer 1

0

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;
1
  • 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
    Commented Feb 3, 2013 at 6:33

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.