MySQL code fails to display category name (WordPress database)

Why does this code fail to display the category name “Apples” using the current WordPress taxonomy system? The Category names are stored in the $wpdb->terms table (wp_terms).

<?php

$ra_category_id = 3; 
$ra_category = $wpdb->get_results("SELECT name FROM $wpdb->terms WHERE term_id = '3'");
$ra_category_name = $ra_category->name;         

?>

<h3>Category: <?php echo $ra_category_name; ?></h3>

The table rows are

term_id     name              slug          term_group
1         Uncategorized     uncategorized   0
2         Blogroll          blogroll        0
3         Apples            apples          0
4         Bananas           bananas         0

Related posts

Leave a Reply

1 comment

  1. $ra_category is the following array:

    array(1) {
      [0]=>
      object(stdClass)(1) {
        ["name"]=>
        string(8) "Apples"
      }
    }
    

    So what you want is:

    $ra_category_name = $ra_category[0]->name;
    

    When dealing with query results, always check the whole result with a var_dump(), it helps.

    (note that you’re also using $ra_category_id but then hardcoding the value “3” in your query)