Support » Plugins » Hacks » How to JOIN tables with $wpdb

  • I want to JOIN $wpdb->comments with commentmeta. Im only trying to get the field “meta_value”.

    global $wpdb;
    $commentQ = "SELECT * FROM $wpdb->comments " . $whereClause . $orderBy;
    $comments = $wpdb->get_results( $commentQ, ARRAY_A);
    // build the string
    $commentString = "";
    $count = 0;
    if( $comments ){
        $first = true;
        foreach( $comments as $row ){
            if( $first ){
                // column labels
                foreach ( $row as $col => $value ){
                        //$commentString .= $col . chr( 9 );

    After i join the tables i want to get “meta_value” from wp_commentmeta_88 and insert it instead of “comment_author”

    // Column Author
                    if($col == 'comment_author') {
                    $commentString .= 'User Meta        ';
                // Column IP
                if($col == 'comment_author_IP') {
                    $commentString .= '   IP    ';
                // Column date
                if($col == 'comment_date') {
                    $commentString .= '                Datum    ';
                // Column comment
                if($col == 'comment_content') {
                    $commentString .= '                Meddelande   ';
                $commentString .= chr( 13 );
                $first = false;

    [please remember to mark any posted code – ]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz


    Why not use get_comment_meta()? I would directly answer you question, but I don’t know how to do table joins either 🙁

    This is a pretty generic MySQL join that will work with what you want. You might have to change the fields that you are looking for, condtions, etc.

    SELECT m.meta_value AS meta_value
    FROM wp_commentmeta AS m
    INNER JOIN wp_comments AS c
    ON c.comment_id = m.comment_id
    AND c.comment_id = 1234

    Im not using get_comment_meta() because this is a plugin to export comments I’m editing. And i was told to not use get_meta.


    Could you explain little more ?
    Is your solution the same solution as what I’m trying to do ?
    Or is it a different way but do the same ?

    From what you said at the start, yes this is exactly what you are trying to do. It will grab the meta_value from the DB that relates to the comment. You just have to change the ID’s that you use to get the value that you want.

    Look at my second block of code where i output the fields to export etc comment_author.

    Im going to remove that comment_author and i want the value of meta_value for the other table.

    Så basicly i understan your INNER JOIN but i dont understand wich id i should put down there.

    If you understand the join then the ID should be easy to see. The one that you need to look at is ‘c.comment_id’. That’s the ID of the comment that you want to get the vlaue for so that’s the one that you need to change.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to JOIN tables with $wpdb’ is closed to new replies.