WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to select some tags with the same term_group, I tested it in phpadmin, the query get the correct result, but once add $wpdb flavor, it returns an empty array. Any advice?

$primary_tag_id = // a function to return term_group, result is 3, comfirmed
    $terms = $wpdb->get_col( $wpdb->prepare(
            "SELECT name
            FROM $wpdb->wp_terms
            WHERE term_group = %d
            ORDER BY name ASC", $primary_tag_id
share|improve this question
up vote 1 down vote accepted

Try this, it worked for me:

    global $wpdb;
    $terms = $wpdb->get_col( $wpdb->prepare(
            "SELECT name
            FROM {$wpdb->terms}
            WHERE term_group = %d
            ORDER BY name ASC", primary_tag_id

What you are after is $wpdb->terms not $wpdb->wp_terms

share|improve this answer
Thanks! Got the result now. – Jenny Jan 12 '12 at 11:41

Your Answer


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.