WordPress.org

Ready to get started?Download WordPress

Forums

How to JOIN tables with $wpdb (7 posts)

  1. Meiioo
    Member
    Posted 1 year ago #

    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 - http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

  2. bcworkz
    Member
    Posted 1 year ago #

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

  3. michael.mariart
    Member
    Posted 1 year ago #

    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
  4. Meiioo
    Member
    Posted 1 year ago #

    bcworkz:
    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.

    michael.mariart:

    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 ?

  5. michael.mariart
    Member
    Posted 1 year ago #

    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.

  6. Meiioo
    Member
    Posted 1 year ago #

    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.

  7. michael.mariart
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic