WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 am trying to display comments by specific users at a custom area. i am successful to do that, but the problem is the permalink structure . this code gets the url as permalinks disabled, if i enable the permalinks from my settings , then these urls starts giving a 404.

here is the code im using :

if(get_query_var('author_name')) :
$curauth = get_userdatabylogin(get_query_var('author_name'));
else :
$curauth = get_userdata(get_query_var('author'));
$querystr = "
    SELECT comment_ID, comment_post_ID, post_title, comment_content
    FROM $wpdb->comments, $wpdb->posts
    WHERE user_id = $uid
    AND comment_post_id = ID
    AND comment_approved = 1
    ORDER BY comment_ID DESC LIMIT 5
 $comments_array = $wpdb->get_results($querystr, OBJECT);
if ($comments_array): ?>
<? foreach ($comments_array as $comment):
    echo "<li><a href='". get_bloginfo('url') ."/?p=".$comment->comment_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
endforeach; ?>
<? endif; ?>

So this gets the url as www.mysite.com/p?=xxxx .. if permalinks are enabled as mysite.com/post-perma-links/ it starts giving 404 on these links generated via the above code. rest of the function is workign fine,

SO possible to get the permalink url in this case instead of the p?=xxx ? help is appreciated cheers

share|improve this question
up vote 1 down vote accepted

Hi @Ayaz Malik:

You need to use the function get_comment_link(). I've rewritten your code using some improved techniques and included the function call in place of what you had:

global $wpdb;
$sql =<<<SQL
  INNER JOIN {$wpdb->posts} 
    ON {$wpdb->comments}.comment_post_id={$wpdb->posts}.ID
  AND {$wpdb->comments}.user_id = %d
  AND {$wpdb->comments}.comment_approved = 1
  {$wpdb->comments}.comment_ID DESC
$sql = $wpdb->prepare($sql,$uid);  // $uid is assumed pre-defined before this code
$comments = $wpdb->get_results($sql, OBJECT);
if ($comments) {
  echo '<ul>';
  foreach ($comments as $comment) {
    $link = get_comment_link($comment->comment_ID);
    echo "<li><a href='{$link}'>Comment on {$comment->post_title}</a><br />" .
  echo '</ul>';
share|improve this answer
Bravo my friend ! Hit in first try , thanks a lot :) – Ayaz Malik Jan 30 '11 at 12:38
@Ayaz Malik - I'm rarely good, but when I am I'm really good. :) Glad I could help. – MikeSchinkel Jan 31 '11 at 2:43

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.