0

I have this function:

<?php
function top_comment_authors($amount = 5) {
global $wpdb;
$results = $wpdb->get_results('
    SELECT
    COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    FROM '.$wpdb->comments.'
    WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    GROUP BY comment_author_email
    ORDER BY comments_count DESC, comment_author ASC
    LIMIT '.$amount
);

foreach($results as $result) {
    $output .= "<li><a href=".$result->comment_author_url.">".$result->comment_author."</a> (".$result->comments_count.")</li>";
}

echo $output;
}
?>

So here, I am trying to link to the comment_author_url (the author page), but it does not do a thing. How can this be? What am I doing wrong?

<li><a href=".$result->comment_author_url.">
  • comment_author_url is not the author's page, but his www instead – iEmanuele Jul 27 '13 at 13:41
1

Change your query from:

SELECT
    COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    FROM '.$wpdb->comments.'
    WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    GROUP BY comment_author_email
    ORDER BY comments_count DESC, comment_author ASC
    LIMIT '.$amount

To:

SELECT *,
    COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    FROM '.$wpdb->comments.'
    WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    GROUP BY comment_author_email
    ORDER BY comments_count DESC, comment_author ASC
    LIMIT '.$amount

And then this:

$output .= "<li><a href=".$result->comment_author_url.">".$result->comment_author."</a> (".$result->comments_count.")</li>";

To:

$output .= "<li><a href=".get_author_posts_url($result->user_id).">".$result->comment_author."</a> (".$result->comments_count.")</li>";

Modifying the query grants you to reach all fields, one of these ( user_id ), will be used to get the author's page with get_author_posts_url();

Update

I suggest you to use double quote on SQL strings and single quote on HTML. In your code you're trying to concatenate $output with nothing.

So your queries might look like this:

"
    SELECT *,
        COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
        FROM $wpdb->comments
        WHERE comment_author_email != '' AND comment_type = '' AND comment_approved = 1
        GROUP BY comment_author_email
        ORDER BY comments_count DESC, comment_author ASC
        LIMIT $amount
"

Then for $output:

$output = '<ul>';
foreach($results as $result) {
$output .= '<li><a href="'.get_author_posts_url($result->user_id).'">'.$result->comment_author.'</a> ('.$result->comments_count.')</li>';
}
$output .= '</ul>';

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.